① 用c语言编写一个程序,输入一个字符串,统计其中各个字符出现的次数
源程序代码如下:
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS//VS环境下需要,VC不需要
#include<stdio.h>
int main()
{
char c = 0;//定义输入字符变量
int num_count = 0;//数字个数
int bigalp_count = 0;//大写字母个数
int littlealp_count = 0;//小写字母个数
int emp_count = 0;//空格个数
int els_count = 0;//其他字符个数
while((c = getchar()) != '
')//连续输入字符直到输入回车结束
{
if((c >= '0')&&(c <= '9'))//判断是否是数字
{
num_count ++ ;
}
else if ((c >= 'a') && (c <= 'z'))//判断是否是小写字母
{
littlealp_count++;
}
else if ((c >= 'A') && (c <= 'Z'))//判断是否是大写字母
{
bigalp_count++;
}
else if(c == ' ')//判断是否是空格
{
emp_count ++;
}
else //判断是否其他字符
{
els_count ++;
}
}
//输出个数统计值
printf("数字个数:%d
小写字母个数:%d
大写字母个数:%d
",num_count, littlealp_count, bigalp_count);
printf("空格个数:%d
其他字符个数:%d
", emp_count, els_count);
return 0;
}
程序运行结果如下:
(1)c语言统计字符有次数扩展阅读:
其他实现方法:
#include <stdio.h>
#include <ctype.h> //对空白字符的判断,调用了isspace()函数,所以要调用头文件
int main()
{
char str[20]; //这块对输入有所限制了
int num_count=0;
int space_count=0;
int other_count=0;
char *p=str;
gets(str); //接收字符串
while(*p)
{
{
num_count++;
}
else if(isspace(*p)) //用isspace函数来判断是不是空白字符
{
space_count++;
}
else
{
other_count++;
}
p++;
}
printf("num_count=%d
",num_count);
printf("space_count=%d
",space_count);
printf("other_count=%d
",other_count);
return 0;
② 用C语言编写一个程序,输入一个字符串,统计其中各个字符出现的次数
//输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。
#include<stdio.h>
intmain(void)
{
charch;
inta=0,b=0,c=0,d=0;
while((ch=getchar())!=' ')
{
if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z')
a++;
elseif(ch>='0'&&ch<='9')
b++;
elseif(ch=='')
c++;
else
d++;
}
printf("字母=%d 数字=%d 空格=%d 其他字符=%d ",a,b,c,d);
return0;
}
③ C语言 统计字符出现次数
#include <stdio.h>
int main()
{ int i,n=0;
char c,s[100];
gets(s);
c=getchar();
for(i=0; s[i]; i++)
if(c==s[i])n++;
printf("%d",n);
return 0;
}
④ c语言 统计字符个数
要统计英文字母,空格,数字和其他字符的个数,代码如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf("请输入一行字符:>");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
letters++;
}
else if(''==c)
{
space++;
}
else if(c>='0'&&c<='9')
{
digit++;
}
else
{
other++;
}
}
printf("字母的个数:>%d\n空格的个数:>%d\
\n数字的个数:>%d\n其他字符的个数:>%d\n",\
letters,space,digit,other);
system("pause");
return 0;
}
⑤ C语言中统计字符出现的次数
是有些错误,我帮你改了下 ,你自己看看:
# include "stdio.h"
# include "string.h"
main()
{
char a[100],b[100];
int i,n1,n2,k,num,flag;
printf("输入原文:\n");
scanf("%s",a);
fflush(stdin); /* flush the input stream in case of bad input */
printf("输入字符串:\n");
scanf("%s",b);
fflush(stdin); /* flush the input stream in case of bad input */
n1=strlen(a);
n2=strlen(b);
num=0;
for(i=0;i<n1-n2;i++)
{
for(k=i;k<n2+i;k++)
{
flag=1;
if(a[k]!=b[k-i])
{
flag=0;
break;
}
if((k-i)==(n2-1))
{
num++;
i+=n2;
}
}
}
printf("总共出现%d\n",num);
system("pause");
}
首先,最好是在scanf("%s",a);后面加上这句 fflush(stdin);
/* flush the input stream in case of bad input */
其次,你的思路有问题,在第二个for循环里面
第二个if的条件应该改成(k-i)==(n2-1),即检查到了字符数组b的结尾了,说明前面的都匹配,那么才加1,呵呵呵,自己好好想想吧
⑥ c语言统计字符串中每个字符出现的次数
#include<stdio.h>
#include<stdlib.h>
int findsub(char*src,char*s)
{
char*ptr=src,*p=s;//定义两个指针
char*ptr2=src+strlen(src),*prev=NULL;//ptr2为src的末位置指针
int len=strlen(s),n=0;//子串的长度和计数器
for(;*ptr;ptr++)//循环整个串
{
if(ptr2-ptr<len)//如果一开始子串就大于src,则退出
break;
for(prev=ptr;*prev==*p;prev++,p++)//寻找第一个相等的位置,然后从此位置开始匹配子串
{
if(*(p+1)==0||*(p+1)==10)//如果已经到了子串的末尾
{
n++;//自增
p=s;//重新指向子串
break;//退出
}
}
}
return n;
}
int main(void)
{
char a[81],b[81];//定义两个字符数组
printf("输入原字符串:");
fgets(a,81,stdin);//输入
printf("输入子字符串:");
fgets(b,81,stdin);
printf("找到:%d ",findsub(a,b));
system("pause");
return 0;
}
(6)c语言统计字符有次数扩展阅读:
①printf()函数是格式化输出函数,一般用于向标准输出设备按规定格式输出信息。
格式输出,它是c语言中产生格式化输出的函数(在stdio.h中定义)。用于向终端(显示器、控制台等)输出字符。格式控制由要输出的文字和数据格式说明组成。
要输出的文字除了可以使用字母、数字、空格和一些数字符号以外,还可以使用一些转义字符表示特殊的含义。
简单点理解就是,在一段程序中你正确使用该函数,可以在该程序运行的时候输出你所需要的内容。
②printf函数是一个标准库函数,它的函数原型在头文件“stdio.h”中。但作为一个特例,不要求在使用printf函数之前必须包含stdio.h文件。
printf()函数的调用格式为:printf("<格式化字符串>",<参量表>)。
其中格式化字符串用于指定输出格式。格式控制串可由格式字符串和非格式字符串两种组成。
⑦ C语言编程:输入一串字母,统计每个字母出现的次数
C语言程序如下:
#include<stdio.h>
int main()
{
char a[100];
char b[24];
int s[100] = { 0 };//用于存储字符的个数
gets(a);//输入字符
//开始比较
for (int x = 0; x < 24; x++)
{
int c = 0;//记录每个字符个数
b[x] = x + 97;//为了让b[0]是a,b[1]是b依次类推
for (int i = 0; i < 100; i++)
{
if (b[x] == a[i])
{
++c;
s[x] = c;
}
}
if (s[x]>=1)//只输出输入中有的字母 的个数
{
printf("%c %d ", b[x], s[x]);
}
}
getchar();
return 0;
}
(7)c语言统计字符有次数扩展阅读:
程序思路:
分为三部分 首先输入字符串 ,其次设定一个字符数组英文小写字母24, 同时设一个int数组 记录个数, 以及一个int c 为了给int数组赋值。最后在输入的时候进行判断,如果字母的值 大于等于1才输出。