c语言中怎样统计字符串中包含英文字母的个数?

编写函数完成以下功能,统计字符串str中饱含的英文字母、空格、数字和其他字符的个数,并将统计结果保存到数组num中,num【0】存英文字母个数,num【1】存,空格个数,num【2】存数字个数,num【3】存其他字符个数。急用,今天就选出最佳答案。谢谢!
*p<='z'&&*p>='a'
请问能这样比较么?有点不确定

c语言中要统计字符串中包含英文字母的个数可以参考以下内容:

main()

{

char str[100],*p;

int num[4],i;

p=str;

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  %d  %d\n",num[0],num[1],num[2],num[3]); 

}

扩展资料:

在写代码的过程中需要注意:

void main()的用法并不是任何标准制定的。 C语言标准语法是int main,任何实现都必须支持int main(void) { /* ... */ }和int main(int argc, char* argv[]) { /* ... */ }。

类似于a+=a++;或者(i++)+(i++)+(i++)属于未定义行为,并不是说c语言中还未定义这种行为,它早有定论,它的结果取决于编译器实现,不要写这样的代码。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2019-08-25

#include<stdio.h>

#include<string.h>

int main( )

{

int i,ch=0,sp=0,num=0,other=0;

char str[50];

gets(str);

for(i=0;i<strlen(str);i++)

{

if(str[i]>='0'str[i]<='9')

{

num++;

}

else if(str[i]>='a'str[i]<='z'||str[i]>='A'str[i]<='Z')

{

ch++;

}

else if(str[i]==' ')

{

sp++;

}

else

{

other++;

}

printf("%c\n",str[i]);

}

printf("Char:%d,Space:%d,Num:%d,Other:%d",ch,sp,num,other);

return 0;

}

扩展资料

其他方法统计字符串中包含英文字母的个数:

#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;

}

本回答被网友采纳
第2个回答  推荐于2019-08-27

#include<stdio.h>

#include<string.h>

int main( )

{

int i,ch=0,sp=0,num=0,other=0;

char str[50];

gets(str);

for(i=0;i<strlen(str);i++)

{

if(str[i]>='0'str[i]<='9')

{

num++;

}

else if(str[i]>='a'str[i]<='z'||str[i]>='A'str[i]<='Z')

{

ch++;

}

else if(str[i]==' ')

{

sp++;

}

else

{

other++;

}

printf("%c\n",str[i]);

}

printf("Char:%d,Space:%d,Num:%d,Other:%d",ch,sp,num,other);

return 0;

}

扩展资料

其他方法统计字符串中包含英文字母的个数:

#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个回答  推荐于2017-10-10
#include <stdio.h>

int count_letter(char *str)
{
    char *p = str;
    int cnt = 0;

    //开始计数
    while (*p != '\0') {
        if ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z')) {
            cnt++;
        }

        p++;
    }
    //计数完成
    
    printf("letter cnt:%d\n", cnt); //打印出英文字母总数

    return cnt; //计数结果返回
}

int main()
{
    char *str = "gkdial9-1.;J19D-=-=YdlUImf"; //实例字符串
    count_letter(str); //调用计数函数
    return 0;
}


以上源码。


主要思路为循环到字符串结尾,逐字符判断是否属于字母范围(A到Z或a到z),如果为字母则计数器+1,直到字符为结束符'\0'为止,表示字符串结束,并将结果返回给函数调用者。

第4个回答  2019-12-23
c语言中要统计字符串中包含英文字母的个数可以参考以下内容: main() { char str[100],*p; int num[4],i; p=str; 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 %d...

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网