int main(){
char s[100]={0};
char *p=s;
int num=0,flag=0,wnum=0,line=0;
while ((gets(s))!=EOF) {
if ((*p>='a'||*p<='z')||(*p>='Z'||*p<='A')) {
wnum++;
if (flag==0) {
*p-=32;
}
flag=1;
}
else{
if (flag==1){
num++;
flag=0;
}
if (*p=='\n'||*p=='\0') {
line++;
}
}
p++;
}
int ave=wnum/num;
printf("Number of lines: %d\nNumber of words: %d\nAverage length of a word: %d",line,num,ave);
}
输入多行之后,command+z还是结束不了输入
请问问题在哪里?
有什么更好的接受多行文字的方法吗?
这是原题,希望不是我错误理解题意了……
谢谢...
Your main problem is that eof is entered incorrectly. It is ctrl z under win, but not under mac, but ctrl d. Note that it is not common
The author tries to output
printf
beforenum
, there is a high probability that it is equal to 0.The most serious problem:
1.
gets(s)
Return one line at a time instead of returning all characters before EOF at once! The poster may have thought this wrong. If not, please read the analysis below.2. Why num=0?
gets(s)
Read one line each time, s is a string. The author only judged one character *p each time, and then continued the next cycle. The input example is likely to cause num =0 means that the num++ sentence will not be executed. Another loopwhile(*p!='
This question is answered by the book written by the father of the C language.