编程题:查找字符在字符串中出现的次数
输入字符串: noobdream 输入要查找的字符: o 字符 o 在字符串中出现的次数为 2
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include <windows.h> #define N 50 int main(void) { char string[N],word; puts("请输入字符串"); gets(string); int n = strlen(string); string[n] = '\0'; puts("请输入要查找的字符"); scanf("%c", &word); int i = 0,flag=0; while (string[i] != '\0') { if (string[i] == word) { flag++; } i++; } printf("字符%c出现%d次\n",word,flag);
return 0; }
#include<iostream>
using namespace std;
int count(char * source,char target)
{
if(!*source)
return 0;
}
if(*source==target)
return 1+count(source+1,target);
return 0+count(source+1,target);
int main()
char string[1024]={0};
char target;
cin>>string;
cin>>target;
cout<<count(string,target)<<endl;
#include <stdi...
用户登录可进行刷题及查看答案
#include <stdio.h> int main() { char str[1000], ch; int i, frequency = 0; printf("输入字符串: "); fgets(str, (sizeof str / sizeof str[0]), stdin); printf("输入要查找的字符: "); scanf("%c",&ch); for(i = 0; str[i] != '\0'; ++i) { if(ch == str[i]) ++frequency; } printf("字符 %c 在字符串中出现的次数为 %d", ch, frequency); return 0; }
登录后提交答案