(1)输入一行字符,分别统计出其中的英文字母、空格、数字和其他字符的个数
#include "stdio.h"
void main()
{
int space=0,letter=0,num=0,other=0;
char c;
//getchar函数获取键盘输入的任何字符
//scanf函数,
while((c=getchar()) !='\n')
{
if(c==32){
space++;
}else if((c>='A' && c<='Z' )||(c>='a' && c<='z')){
letter++;
}else if(c>='0' && c<='9'){
num++;
}else{
other++;
}
}
printf("空格:%d,字母:%d,数字:%d,其他:%d\n",space,letter,num,other);
}
要求C++,所以用C++函数实现
#include <string>
#include <iostream>
using namespace std;
void main()
{
int s=0,l=0,n=0,o=0;
string str;
getline(cin,str);
int len = str.length();
for(int i=0;i<len;i++)
{
if(str[i] >= '0' && str[i] <= '9')
n++;
else if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
l++;
else if(str[i] = ' ')
s++;
else
o++;
}
cout<<"空格数量:"<<s<<endl;
cout<<"数字数量:"<<n<<endl;
cout<<"字母数量:"<<l<<endl;
cout<<"其它数量:"<<o<<endl;
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632