c语言怎么编写统计一个c语言字符串函数中各个字符的个数

中没有布尔类型,所以用0代表false,非0代表true
所以上面的代码,当s[i] == 0时,循环结束
顺带一说,'0'和'\0'的值都为0
您的举报已经提交成功,我们将尽快处理,谢谢!
求出当s[i]为数字(0-9)时,字符串s中从s[i+1]到字符串最后一共有多少个字符,统计结果在n中
大家还关注
(window.slotbydup=window.slotbydup || []).push({
id: '2081942',
container: s,
size: '1000,60',
display: 'inlay-fix'8747人阅读
c语言(42)
假定每个单词用空格隔开。
输入:how are you!
两种方法:
#include &stdio.h&
#include &string.h&
#define SIZE 20
int main()
char str[SIZE]={'\0'};
int count=0;
printf(&please input the string\n&);
gets(str);
puts(str);
int length = strlen(str);
for (int i=0;i&i++)
//判断是不是空格不是的话在while里面i++执行判断到下一个空格的出现或是结束
if(str[i]!=' ')
while(str[i]!=' '&&str[i]!='\0')
printf(&%d\n&,count);
#include &stdio.h&
#include &string.h&
#define SIZE 20
int main()
char str[SIZE]={'\0'};
int count=0;
int flag=0;
printf(&please input the string\n&);
gets(str);
puts(str);
int length = strlen(str);
for (int i=0;i&i++)
//判断是不是空格,是的话flag=0,
//不是的话判断前面是不是空格即flag是否等于0,
//是空格的话说明是新单词的开始
if(str[i]==' ')
if(flag==0)
printf(&%d\n&,count);
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:318533次
积分:4353
积分:4353
排名:第3687名
原创:117篇
转载:40篇
评论:70条
(3)(8)(6)(5)(8)(11)(10)(8)(36)(26)(18)(19)c语言中怎样统计字符串中包含英文字母的个数?_百度知道
c语言中怎样统计字符串中包含英文字母的个数?
#include&&stdio.h&int&count_letter(char&*str){&&&&char&*p&=&&&&&int&cnt&=&0;&&&&//开始计数&&&&while&(*p&!=&'\0')&{&&&&&&&&if&((*p&&=&'a'&&&&*p&&=&'z')&||&(*p&&=&'A'&&&&*p&&=&'Z'))&{&&&&&&&&&&&&cnt++;&&&&&&&&}&&&&&&&&p++;&&&&}&&&&//计数完成&&&&&&&&printf(&letter&cnt:%d\n&,&cnt);&//打印出英文字母总数&&&&return&&//计数结果返回}int&main(){&&&&char&*str&=&&gkdial9-1.;J19D-=-=YdlUImf&;&//实例字符串&&&&count_letter(str);&//调用计数函数&&&&return&0;}以上源码。主要思路为循环到字符串结尾,逐字符判断是否属于字母范围(A到Z或a到z),如果为字母则计数器+1,直到字符为结束符'\0'为止,表示字符串结束,并将结果返回给函数调用者。
其他类似问题
3人觉得有用
为您推荐:
提问者采纳
main(){ char str[100],*p; int num[4],i; p= gets(str); for(i=0;i&4;i++)
num[i]=0; for(;*p!='\0';p++) {
if((*p&='z'&&*p&='a')||(*p&='Z'&&*p&='A')) num[0]++;
else if(*p==' ') num[1]++;
else if((*p&='9'&&*p&='0')) num[2]++;
else num[3]++; } printf(&%d
%d\n&供互垛就艹脚讹协番茅,num[0],num[1],num[2],num[3]); }应该看得懂的吧!~~~~~
提问者评价
这个似乎更切实,不用去查asc码对照表了
其他1条回答
逐个字符判断。英文字母asc码65~90&97~122,数字48~57,空格32,如下:num[0]=0;num[1]=0;num[2]=0;num[3]=0;for (i=0;i&strlen(str);i++){
if (((str[i]&=65供互垛就艹脚讹协番茅)&&(str[i]&=90))||((str[i]&=97)&&(str[i]&=122))) num[0]++;//字母
else if (str[i]==32) num[1]++;//空格
else if ((str[i]&=48)&&(str[i]&=57)) num[2]++;//数字
else num[3]++;//其他}
英文字母的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁编写C语言程序统计输入字符串的个数、_百度知道
编写C语言程序统计输入字符串的个数、
第一种方法:统计输入字符串的长度#include&stdio.h&#include&string.h&int main(){
char buff[100]={0}; printf(&请输入一个字符串:\t&);
scanf(&%s&,&buff);
printf(&你输入字符串的个数为:\t%d\n &,strlen(buff));}第二种方法:遍历输入字符串,直到结束字符'\0'#include&stdio.h&void main(){ char str[1000],*p; int word=0; printf(&请输入一个字符串:\t&); gets(str);
for(p=*p!='\0';p++)
printf(&输入字符串的长度为:\t%d\n&,word); }
其他类似问题
为您推荐:
return 0; &
printf(&quot.h&getchar(); &quot,&quot,num);
gets(str);getchar();))
while(strtok(NULL;))
num++;.h&n&#include&%d\string,&
if(strtok(
char str[10000];int main(){
int num = 1;stdio#include&lt
c语言程序的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁

我要回帖

更多关于 c语言字符串 的文章

 

随机推荐