题目:
编写一个程序,从键盘上输入一篇英文文章。文章的实际长度随输入变化,最长有10行,每行80个字符。要求分别统计出其中的英文字母、数字、空格和其它字符的个数。(提示:用一个二维字符数组存储文章)
代码:
#include <iostream>
using namespace std;
int main()
{
int i, j, english = 0, number = 0, space = 0, other = 0;
char a[10][80];
cout << "请输入一篇英文文章" << endl;
cout << "(每行满80个字符后自动换行,若想手动换行请按回车键)" << endl << endl;
for (i = 0; i < 10; ++i)
{
cout << "第" << i+1 << "行:";
for (j = 0; j < 80; ++j)
{
cin.get(a[i][j]);
if (a[i][j] == '\n') break;
else if (((a[i][j] >= 65) & (a[i][j] <= 90)) | ((a[i][j] >= 97) & (a[i][j] <= 122))) english = english + 1;
else if ((a[i][j] >= 48) & (a[i][j] <= 57)) number = number + 1;
else if (a[i][j] == 32) space = space + 1;
else other = other + 1;
}
}
cout << endl << "本篇英文文章中,英文字母有" << english << "个,数字有" << number << "个,空格有" << space << "个,其它字符有" << other << "个" << endl << endl;
system("pause");
return 0;
}
版权声明:本文为weixin_41013202原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。