實(shí)驗(yàn)6-2 分類(lèi)統(tǒng)計(jì)字符個(gè)數(shù) (15 分)
1. 題目摘自
https://pintia.cn/problem-sets/13/problems/474
2. 題目?jī)?nèi)容
本題要求實(shí)現(xiàn)一個(gè)函數(shù),統(tǒng)計(jì)給定字符串中英文字母、空格或回車(chē)雷逆、數(shù)字字符和其他字符的個(gè)數(shù)嚣鄙。
函數(shù)接口定義:
void StringCount( char s[] );
其中 char s[] 是用戶(hù)傳入的字符串胞此。函數(shù)StringCount須在一行內(nèi)按照
letter = 英文字母?jìng)€(gè)數(shù), blank = 空格或回車(chē)個(gè)數(shù), digit = 數(shù)字字符個(gè)數(shù), other = 其他字符個(gè)數(shù)
的格式輸出沥阳。
輸入樣例:
aZ &
09 Az
輸出樣例:
letter = 4, blank = 3, digit = 2, other = 1
3. 源碼參考
#include <iostream>
using namespace std;
#define MAXS 15
void StringCount( char s[] );
void ReadString( char s[] );
int main()
{
char s[MAXS];
ReadString(s);
StringCount(s);
return 0;
}
void ReadString(char s[])
{
memset(s, 0, MAXS);
cin.read(s, 10);
return;
}
void StringCount(char s[])
{
char ch;
int n = strlen(s);
int letter = 0, blank = 0, digit = 0, other = 0;
for (int i = 0; i < n; i++)
{
ch = s[i];
if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))
{
letter++;
}
else if ((ch >= '0') && (ch <= '9'))
{
digit++;
}
else if ((ch == ' ') || (ch == '\n'))
{
blank++;
}
else
{
other++;
}
}
cout << "letter = " << letter << ", blank = " << blank << ", digit = " << digit << ", other = " << other << endl;
return;
}