刚开始用了个笨方法,用fgets输入所有字符串后,计算长度。第二种是输入时用scanf循环一个一个的输入字符串,并在这个过程计算每个单词的长度和个数
要注意fgets函数会读入enter键,‘\n’。而scanf函数遇到空格即会停止输入。熟悉字符串的处理函数
/*#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#pragma warning(disable:4996)
void main()
{
char s[1000];
char ch;
int n=0,i,j;
int num=0,l=0;
/* while(1)
{
scanf("%c",&ch);
if(ch=='\n'||ch==EOF)
break;
else
s[n++]=ch;
}*/
/*fgets(s,sizeof(s),stdin);
int len=strlen(s);
printf("%s",s);
for(i=0;i<len;i++)
{
if((s[i+1]==' '||s[i+1]=='\n')&&isalpha(s[i]))
num++;
}
for(j=0;j<len;j++)
{
if(isalpha(s[j]))
l++;
}
double result=(l+0.0)/(num+0.0);
printf("%.2lf",result);
system("pause");
}*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cctype>
#pragma warning(disable:4996)
void main()
{
char s[1000];
int n=0;
int num=0;
int len;
double result;
int a[1000];
while(scanf("%s",s)!=EOF)
{
len=strlen(s);
n+=len;
num++;
}
result=(n+0.0)/num;
printf("%.2lf",result);
system("pause");
}