编写一个程序,输入行字符,分别统计其中英文字母,数字和其他字符的个数,怎么做,求解!

编写一个程序,输入行字符,分别统计其中英文字母,数字和其他字符的个数,怎么做,求解!,第1张

int main()                     //主函数

{char s[100];                  //用于存放字符串的数组

 int l,e,n,t;                  //l是总长,e字母,n数字,t其它

 l=e=n=t=0;                    //初始化

 gets(s);                      //输入字符

 while(s[l]) l++;              //测出字符串长度

 for(l--;l>=0;l--)             //逐个检测字符统计

     if((s[l]>='a'&&s[l]<='z')||(s[l]>='A'&&s[l]<='Z')) e++;

     else if(s[l]>='0'&&s[l]<='9') n++;

          else t++;

 printf("英文字母%d个,数字%d个,其他%d个。\n",e,n,t); //显示结果

 system("PAUSE");                     //暂停

 return 0;}                           //结束

#include "stdioh"\x0d\#include "stdlibh"\x0d\void main()\x0d\{\x0d\ FILE fp;\x0d\ char ch;\x0d\ int charCounts=0,numCounts=0;\x0d\ if((fp=fopen("testtxt","r"))==0)//注意,由于我不知道你想打开的文件的位置和名称,我随意写的一个文件名,你自己要按你文件的路径和名称做修改\x0d\ {\x0d\ printf("文件读取失败!\n");\x0d\ exit(0);\x0d\ }\x0d\ while((ch=fgetc(fp))!=EOF)\x0d\ {\x0d\ if(ch>='0'&&ch回答于 2022-11-16

程序可按照以下流程执行:

1、输入字符串

2、对于字符串中的每一个字符,判断其为何种类型,并将相应的累加计数器加1。对于大小写字母和数字,可通过一个范围(大于等于某值和小于等于某值)来判断

代码如下:

#include <stringh>

#include <stdioh>

int main()

{

char str[1000];

int lowAlpha, upAlpha, num, other;

int i;

lowAlpha = upAlpha = num = other = 0;

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

if (str[i] >= 'a' && str[i] <= 'z')

lowAlpha++;

else if (str[i] >= 'A' && str[i] <= 'Z')

upAlpha++;

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

num++;

else

other++;

printf("Upper:%d, Lower:%d, Number:%d, Other:%d\n", upAlpha, lowAlpha, num, other);

return 0;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

int abcCount=0;//英文字母个数

int spaceCount=0;//空格键个数

int numCount=0;//数字个数

int otherCount=0;//其他字符个数

Scanner scan=new Scanner(Systemin);

String str=scannextLine();

char[] ch = strtoCharArray();

for(int i=0;i<chlength;i++){

if(CharacterisLetter(ch[i])){

//判断是否字母

abcCount++;

}

else if(CharacterisDigit(ch[i])){

//判断是否数字

numCount++;

}

else if(CharacterisSpaceChar(ch[i])){

//判断是否空格键

spaceCount++;

}

else{

//以上都不是则认为是其他字符

otherCount++;

}

}

Systemoutprintln("字母个数:"+abcCount);

Systemoutprintln("数字个数:"+numCount);

Systemoutprintln("空格个数:"+spaceCount);

Systemoutprintln("其他字符个数:"+otherCount);

很简单了,代码如下:

#include

void

main()

{

char

a[100];

int

i,count=0,flag=1,n=0;

gets(a);//从键盘输入字符串

for(i=0;a[i];i++)

{

if((a[i]>='a'

&&

a[i]<='z')

||

(a[i]>='a'

&&

a[i]<='z'))//判断是不是字符

{

flag=0;//设置字符标志

continue;//返回下一个循环

}

if(!flag)//前面一位是字符,即当前是某个单词字符结束

{

count++;//统计单词个数

flag=1;//设置非字符标志

}

}

i--;

if((a[i]>='a'

&&

a[i]<='z')

||

(a[i]>='a'

&&

a[i]<='z'))

count++;//增加字符串结束的单词

printf("共有%d单词\n",count);

}

#include

<iostream>

using

namespace

std;

int

main()

{

char

ch=new

char[];//创建一个长度不确定的字符串

cout<<"请输入一串字符:"<<endl;

cin>>ch;//输入字符

int

a=0;//小写字母的个数

int

b=0;//大写字母的个数

int

c=0;//数字的个数

int

d=0;//其它字符的个数

for(int

i=0;ch[i]!='\0';i++)

{

if(ch[i]>='a'&&ch[i]<='z')

a++;

else

if(ch[i]>='A'&&ch[i]<='Z')

b++;

else

if((int)ch[i]>=48&&(int)ch[i]<=57)

c++;

else

d++;

}

cout<<"其中,小写字母有"<<a<<"个,大写字母有"<<b<<"个,数字有"<<c<<"个,其它标点有"<<d<<"个!"<<endl;

return

0;

}

以上就是关于编写一个程序,输入行字符,分别统计其中英文字母,数字和其他字符的个数,怎么做,求解!全部的内容,包括:编写一个程序,输入行字符,分别统计其中英文字母,数字和其他字符的个数,怎么做,求解!、用c语言编写一个程序,来计算文件中字符、数字的个数、C语言(简单的)编写程序输入任意一串字符统计其中大写字母,小写字母。数字及其他字符的个数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10213387.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存