C和指针之字符串编程练习11(统计一串字符包含the的个数)
【摘要】 1、问题
编写一个函数,对标准的输入进行扫描,并对单词"the"出现的次数进行计数,区分大小写, 输进来的输入可以包含空格字符等等
2、代码实现
#include <stdio.h>#include <string.h> /**编写一个函数,对标准的输入进行扫描,并对...
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
文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。
原文链接:chenyu.blog.csdn.net/article/details/78595909
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)