xiahuanjun789 2015-03-17 01:06 采纳率: 100%
浏览 2380
已采纳

C++ 统计一串字符各种字符的数

头文件
#include
#include
#include
#include
#include
void count(char a[]);
using namespace std;

void count(char a[])
{
int D_num=0,X_num=0,num=0,spen=0,other=0;

int i;
for(i=0;i<=(strlen(a)-1);i++)
{
    if(a[i]>='a' && a[i]<='z')
        X_num++;
    else if(a[i]>='A' && a[i]<='Z')
        D_num++;
    else if(a[i]>='0' && a[i]<='9')
        num++;
    else if(a[i]==' ')
        spen++;
    else
        other++;
}
cout<<"小写字符为:"<<X_num;
cout<<"大写字母为:"<<D_num;
cout<<"数字为:"<<num;
cout<<"空格为:"<<spen;
cout<<"其他为:"<<other<<endl;

}

.CPP
#include "stdafx.h"

void main()
{
int i;
char a[1000];

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

for(i=0;a[i]=getchar()!='\n';i++){}

cout<<"核查如下:"<<endl;

count(a);
_getch();

}
为什么运行结果 只有把其他统计 数字什么的不统计 哪里有问题

  • 写回答

3条回答 默认 最新

  • 罗博士 2015-03-17 01:47
    关注

    主函数里for写成这样试试:
    for(i=0;(a[i]=getchar()) != '\n';i++)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题