#include "stdio.h"
main()
{
char string[81];
int i,num=0,word=0;
char c;
gets (string);
for (i=0;(c=string)!="\0";i++);
if (c==" ") word=0;
else if (word==0)
{
word=1;
num++;
}
printf("There are %d words in the line\n",num);
}
编译我用的 gcc -o 6.8.out 6.8.c
提示:
6.8.c: In function `main':
6.8.c:8: warning: comparison between pointer and integer
6.8.c:9: warning: comparison between pointer and integer
/tmp/ccMve2Ss.o(.text+0x32): In function `main':
: warning: the `gets' function is dangerous and should not be used
#include "stdio.h"
main()
{
char string[81];
int i,num=0,word=0;
char c;
gets (string);
for (i=0;(c=string)!="\0";i++){
if (c==" ") word=0;
else if (word==0)
{
word=1;
num++;
}
}
printf("There are %d words in the line\n",num);
}
改成上面那样,经我验证,正常