大一下期末考试:统计一行字符串中单词的个数

来源:互联网 发布:sql server2014安装包 编辑:程序博客网 时间:2024/06/06 16:37

大一下期末考试:统计一行字符串中单词的个数

#include <stdio.h>#include <stdlib.h>#define N 100int main(){    char strings[N];    char i,sum=1;    printf("please input a string: ");    gets (strings);    if(strings[0]=='\0')    {        printf("there is nothing");    }    if(strings[0]==' ')    {        printf("sorry ,first word can't be blank");    }    else    {        for (i=0; strings[i]!='\0'; i++)        {            if(strings[i]==' ')                sum++;        }        printf("the sum of words is %d",sum);    }}

洛水成诗

原创粉丝点击