題目:
編寫一個程式,從鍵盤上輸入一篇英文文章。文章的實際長度随輸入變化,最長有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個字元後自動換行,若想手動換行請按Enter鍵)" << 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;
}