数文本串中的单词数

我怕爱的太早我们不能终老 提交于 2019-12-01 05:33:04
 1 int Count_word(char *s){
 2     int state;
 3     if(s[0]==' ') state=0;
 4     else state=1;
 5     int ans=0;
 6     for(int i=1;s[i];i++){
 7         if(s[i]==' ') {
 8             if(state==1){
 9                 ans++;
10                 state=0;
11             }
12         }
13         else {
14             if(state==0){
15                 state=1;
16             } 
17         }
18     } 
19     if(state==1) ans++;
20     return ans;
21 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!