C语言编写程序:给定字符串"Thisisastring",计算字符串中的所含有的指定字符的个数(指定字符通过键盘输入),返回此值。
运行效果:
请输入待查找字符:s
字符串Thisisastring中包含3个字符s
C语言编写程序:给定字符串"Thisisastring",计算字符串中的所含有的指定字符的个数(指定字符通过键盘输入),返回此值。
运行效果:
请输入待查找字符:s
字符串Thisisastring中包含3个字符s
#include <stdio.h>
int main()
{
char s[] = "Thisisastring";
char ch;
int i = 0, count = 0;
printf("请输入待查找字符:");
scanf("%c", &ch);
while(s[i] != '\0') {
if(s[i] == ch) {
count++;
}
i++;
}
printf("字符串%s中包含%d个字符%c", s, count, ch);
return 0;
}