输入一行字符,统计其中的英文字母,空格,数字的个数[size=24px][/size]

....小弟疑惑
输入一行字符,统计其中的英文字母,空格,数字的个数[size=24px][/size]
 
 

曰。。#include<stdio.h>

void main()

{

char m[1000]; int i,num=0,alpha=0,other=0,space=0;

printf("请输入:");

 scanf("%s",&m);

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

{ if ('A'<=m[i] && m[i]<='Z'||'a'<=m[i] && m[i]<='z')alpha++; else if(m[i]==' ')space++; else if ('0'<=m[i] && m[i]<='9')num++; else other++; }

 printf("数字=%d,字母=%d,空格=%d,其他=%d",num,alpha,space,other); }

大神日。。

#include<stdio.h>
#include<string.h>
void main()
{
char m[1000];
int i,num=0,alpha=0,other=0,space=0;
printf("请输入:");
gets(m);[color=#FF9900][/color]
for(i=0;m[i];i++)
{
if ('A'<=m[i] && m[i]<='Z'||'a'<=m[i] && m[i]<='z')alpha++;
else if(m[i]==' ')space++;
else if ('0'<=m[i] && m[i]<='9')num++;
else other++;
}
printf("数字=%d,字母=%d,空格=%d,其他=%d",num,alpha,space,other);

}

c++大神答。。。

#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
  string str;
  int iword=0,ispace=0,idigit=0;
  cout<<"请输入一行字符"<<endl;
  getline(cin,str);
  for(string::size_type ix=0;ix!=str.size();++ix)
  {
  if(isalpha(str[ix])) ++iword;
  else if(isspace(str[ix])) ++ispace;
  else if(isdigit(str[ix])) ++idigit;
  }
  cout<<"字母有"<<iword<<"个"<<endl;
  cout<<"空格有"<<ispace<<"个"<<endl;
  cout<<"数字有"<<idigit<<"个"<<endl;
  return 0;
}

#include<stdio.h>
#include<string.h>
#define SIZE 24
int main(void)
{

  char str[SIZE];
char *i=str;
int ch=0,space=0,num=0,other=0;
gets(str);

for(;*i!='\0';i++)
{
if(*i>='a'&&*i<='z'||*i>='A'&&*i<='Z')
ch++;
else if (*i==' ')
space++;
else if(*i>='0'&&*i<='9')
num++;
else  
other++;
}
printf("chars number is %d\n",ch);
printf("space number is %d\n",space);
printf("digit number is %d\n",num);
  printf("other number is %d\n",other);
  return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值