从键盘输入一串字符串,统计字符串中特定字符的个数,并输出个数

从键盘输入一串字符串,统计字符串中特定字符的个数,并输出个数

程序设计思路如下:

从键盘分别输入字符串和要统计的字符,然后对此字符串从头开始逐个与所统计的字符比较,如相同,则让计数器加1,知道字符串整体比较结束为止,计数器中就是需统计的字符的个数。

#include "stdio.h"

main()
{ char str[100],ch;  /*定义str字符串,定义ch用来存放要统计的字符*/
  int cnt=0,i;       /*定义cnt用作计数器,初始值为0*/
  
  /*输入字符串*/
  printf("请输入字符串:");    
  gets(str);
  
  /*输入要统计的字符*/
  printf("请输入要统计的字符:");
  scanf("%c",&ch);
  
  /*对此字符串从头开始逐个与所统计的字符比较,如相同,则让计数器加1,知道字符串整体比较结束为止*/
  for( i=0;str[i];i++ )
     if( str[i]==ch )
         cnt++;
  
  /*输出结果*/       
  printf("%s串中%c字符的个数是:%d个",str,ch,cnt);
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-08
#include <stdio.h>
#include <string.h>
#define MAX 100
int main()
{
char num[MAX]={0};
char c=0;
int len,cnt=0;
scanf("%s %c",num,&c);
len=strlen(num);
while(len--)
{
if(c==num[len]) cnt++;
}
printf("%d\n",cnt);
    return 0;
}

第2个回答  2016-05-26
#include #include using namespace std;int main(){char str[100];cout<<"输入一个字符串"<
第3个回答  2016-07-09
主要代码:
char s[1000];
int i,j=0;
scanf("%s",s);
for(i=0;i<1000;i++)
{
if(s[i]=='*') //for循环查找字符*的数量
j++;//利用变量j计数
}
相似回答