c语言字符计数
1. c语言中统计字符串中各个字符的个数
原发布者:zlaikai1314
#include#include#include#includeusingnamespacestd;chara[100];//字符数组intb[100];//字符个数doublep_a[100];//字符概率数组intsum=0;//字符总数//判断当前字符temp是否已出现过boolsearch(chartemp,chara[],intm,intn)//m为数组a的元素总个数,即100;n为当前数组a中存放的字符种类的个数{inti=0;while(i<n){if(a[i]==temp)returntrue;elsei++;}returnfalse;}//求各个字符的个数,放在数组b中voidread_file(stringfile_name="test_data.txt"){intk=0;ifstreamfile(file_name.c_str());//将string转化为char数组chartemp;if(file.is_open()==true)//检查文件是否打开{while(file.peek()!=EOF)//从文件中读取一个字符,但该字符并未从输入流中删除{file.get(temp);//从文件读入一个字符并把它存储在tempsum++;//统计出现的字符总数if(search(temp,a,100,k)){for(inti=0;i<k;i++){if(temp==a[i]){b[i]++;break;}}}else
2. 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;
}
3. C语言字符统计
#include<stdio.h>
intmain()
{inti,a,n=0;
charc,s[101];
scanf("%c%d%*c",&c,&a);
gets(s);
if(a==0&&c>='a'&&c<='z')c-=32;
for(i=0;s[i];i++)
if(c==(a==0&&s[i]>='a'&&s[i]<='z'?s[i]-32:s[i]))n++;
printf("%d
",n);
return0;
}
4. 字符计数 c语言
统计输入的字符总数
比如输入aaa^Z可以打印出总数3
之所以没看到打印是因为窗口被关闭了
可以在结尾printf后加上
system("pause");
#include
main()
{
long
nc;
nc=0;
while(getchar=())
!=EOF)
++nc;
printf("%d\n",nc);
system("pause");
}
5. C语言字符串计数
#include<stdio.h>
intmain(void)
{
charaChar;
intspaces=0,upper=0,lower=0,other=0;
do{
scanf("%c",&aChar);//读取字符
if(aChar=='')//判断是否空格
{
++spaces;
continue;
}
if('A'<=aChar&&aChar<='Z')//判断是否大写字母
{
++upper;
continue;
}
if('a'<=aChar&&aChar<='z')//判断是否小写字母
{
++lower;
continue;
}
++other;//以上都不是,则是其他字符
}while(aChar!=' ');
printf("spaces=%d,upper=%d,lower=%d,other=%d ",
spaces,upper,lower,other-1);
return0;
}
因为是用' '来判断一行字符串的,而 也是一个字符,导致最后统计other会多1个,所以要other-1,也可以加一个if语句来判断:
if(aChar!=' ')
++other;
当然,就算把
也统计进去,也是符合题意的。
6. c语言怎么计算字符串的字符个数
一般有三种办法可以计算英文字符的个数:
1)使用strlen()函数
2)从首字符开始,边扫描边计数,到'\0'为止('\0'不计数)
3)从首字符开始,扫描到'\0'为止,'\0'地址与字符串首地址的差。
7. c语言如何统计字符个数
在C语言中,要统计一个字符串的字符个数,可以采用char类型的字符数组,再进行逐个字节的扫描,如果它的ASCII值大于零,这个字节算一个字符;如果它的ASCII值小于零的,就连同后续的一个字节算一个字符。遇到ASCII值等于零,就停止统计输出统计的结果。
8. C语言 字符计数
EOF是判断读取文件是否是结尾,但是你现在这个不是读取文件,所以,这个应该是个死循环,最好是换成getchar!='\n';一般输入完成后是回车所以判断它是不是输入回车了,输入回车了,那么就是输入结束了,然后nc就是你输入的内容的字符个数while就是个循环,
9. C语言中怎么统计字符个数
可以通过switch语句,把字母列出来,然后每个字母设置一个变量,遍历数组,对不同字母的变量每次遇到加加就行了