习题6-8 统计一行文本的单词个数 (15分)

习题6-8 统计一行文本的单词个数 (15分)

#include<stdio.h>  
int main()  
{  
    char s[1001];  
    gets(s);  
    int count=0;  
    int i=0;  
    while(s[i]==' ') //跳过开头的空格  
        i++;  
    while(s[i]!='\0')  
    {  
        if(s[i]!=' ')  
        {   
            count++;  
            while(s[i]!=' ') //跳过一个单词的其他字符  
            {  
                if(s[i]=='\0')  //这句很重要,漏掉后出现严重错误  
                    break;//最后一个单词 
                else{
                	i++;
                }    
            }  
        }  
        else  
        {  
            while(s[i]==' ')  
                i++;      
        }  
    }  
    printf("%d\n",count);  
}
发布了137 篇原创文章 · 获赞 3 · 访问量 4876

猜你喜欢

转载自blog.csdn.net/qq_38054511/article/details/104203581