Fork me on GitHub

P_166统计单词数

 1 //统计单词数 
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     char string[81];
 9     int i,num=0,word=0;
10     char c;
11     gets(string);
12     for(i=0;(c=string[i])!='\0';i++)//不是\0就继续执行循环 
13     {
14         if(c==' ')                //遇见空格,word为0,进入下一个条件 
15         {
16             word=0;
17         }
18         else if(word==0)          //出现空格,word为1 
19         { 
20             word=1;                
21             num++;
22          } 
23     }
24     cout<<num<<endl;
25     return 0;
26  } 

 

posted @ 2018-04-10 15:17  风中等待  阅读(122)  评论(0)    收藏  举报