头文件
#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();
}
为什么运行结果 只有把其他统计 数字什么的不统计 哪里有问题
主函数里for写成这样试试:
for(i=0;(a[i]=getchar()) != '\n';i++)
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
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 << "lowcase" << X_num;
cout << "upcase" << D_num;
cout << "num" << num;
cout << "space" << spen;
cout << "other" << other << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
char * a = new char[1000];
memset(a, 0, 1000);
cout << "please enter:" << endl;
cin >> a;
cout << "result as below" << endl;
count(a);
return 0;
}
for(i=0;(a[i]=getchar()) != '\n';i++)
这是正解。表达式结合性错误,按照lz的写法,a[i]得到的是getchar()) != '\n'这个判断的结果而不是getchar