漢字的ascii碼第一位肯定是1,也就是小於0,而字母的ascii碼第一位一定是0,也就是大於0,可以依此區分
/*
*
*Problem Description
*統計給定文本文件中漢字的個數。
*
*
*Input
*輸入文件首先包含一個整數n,表示測試實例的個數,然後是n段文本。
*
*
*Output
*對於每一段文本,輸出其中的漢字的個數,每個測試實例的輸出佔一行。
*
*[Hint:]從漢字機內碼的特點考慮~
*
*
*
*Sample Input
*2
*WaHaHa! WaHaHa! 今年過節不說話要說只說普通話WaHaHa! WaHaHa!
*馬上就要期末考試了Are you ready?
*
*
*Sample Output
*14
*9
*
*
*Author
*lcy
*
*
*Source
*C語言程序設計練習(五)
*
*
*Recommend
*lcy
*
*/
#include<iostream>
#include<string>
using namespace std;
int cnt;
int num_of_chinese(string s) {
cnt = 0;
for (int i = 0; i < s.size(); i++) {
if (s.at(i) < 0) {
cnt++;
i++;
}
}
return cnt;
}
int main() {
int n;
string s;
cin >> n;
getchar();
for (int i = 0; i < n; i++) {
getline(cin, s);
cout << num_of_chinese(s) << endl;
}
system("pause");
return 0;
}