输入一行字符,统计其中有多少个单词,单词之间用空格分隔开

 
 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #include<string.h>

 4  

 5 int main()  6 {  7   char string[255];  8   int i,num=0,word=0;  9   char c; 10   gets(string); 11   for(i=0;(c=string[i])!='\0';i++) 12   { 13     if(c==' ') 14     word=0; 15     else if(word == 0) 16     { 17       word=1; 18       num++; 19     } 20   } 21   printf("There is %d word(s) in the line.",num); 22   return EXIT_SUCCESS; 23 }

你可能感兴趣的:(字符)