点击(此处)折叠或打开
- #include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
- int main(){
- //初始化计数器
- int counter = 0;
- //初始化存储输入字符串的数组
- char find, a[100];
- //提示输入字符串
- printf("enter the string: ");
- //接收字符串
- scanf("%s", a);
- //清理缓冲区,避免第一次输入时的回车被读入进来
- fflush(stdin);
- //提示输入要查找的字符
- printf("enter the char you want to count: ");
- //接收要查找的字符
- scanf("%c", &find);
- //循环查找每一位上的字符是否与find相符
- for (int i = 0; i < strlen(a); i++){
- //如果相符,则计数器+1
- if (a[i] == find)
- counter += 1;
- }
- //打印总共有多少个find出现在了字符串中
- printf("there are %d %c in this string.\n", counter, find);
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议