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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器