hdu_problem_2030_汉字统计

汉字的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;
}

猜你喜欢

转载自blog.csdn.net/CoderMaximum/article/details/86593446