您的位置:首页 > 编程语言

C和指针之字符串编程练习11(统计一串字符包含the的个数)

2017-11-21 19:19 417 查看

1、问题

编写一个函数,对标准的输入进行扫描,并对单词"the"出现的次数进行计数,区分大小写,

输进来的输入可以包含空格字符等等

2、代码实现

#include <stdio.h>
#include <string.h>

/**
编写一个函数,对标准的输入进行扫描,并对单词"the"出现的次数进行计数,区分大小写,
输进来的输入可以包含空格字符等等
**/

void count_the(char *data)
{
int count = 0;
const char *the = "the";
while ((data = strstr(data, the)) != NULL)
{
++count;
//指针一定要记得后移动,不然会死循环
++data;
}
printf("all has %d count the\n", count);
}

int main()
{

char data[100] = "";
gets(data);
count_the(data);
return 0;
}


3、运行结果

./count
the The chenyuthe thehello hethe thebai
all has 5 count the
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐