#include < string .h> #include <stdio.h> int main() { int count = 1 ; char str[ 1024 ]; int i= 0 ; int j = 0 ; char ch= ' 0 ' ; while (ch != ' \n ' ) //输入一串英文 { ch = getchar(); str[j] = ch; j ++ ; } str[j] = ' \0 ' ; while (str[i]!= ' \0 ' ) //统计单词个数 { if (str[i]== ' ' &&str[i+ 1 ]!= ' ' &&str[i+ 1 ]!= ' \0 ' ) count ++ ; i ++ ; } printf( " %d\n " ,count); }
测试结果:
I am going to shoping! 5 ------------------ (program exited with code: 0 ) Press return to continue