Character Statistics
Subject description:
Statistics for the number of characters in a given text file.
Input
A first input file contains the integer n, the number of test examples, then the n pieces of text.
Output
For each piece of text, wherein the number of output characters, the output of each test case in one row.
[Hint:] Features Chinese machine code from consideration ~
the Sample the Input2 WaHaHa! WaHaHa! This year the feast will not say say only speak Mandarin WaHaHa! WaHaHa! Soon the final exam Are you ready?Sample Output
14 9
By the answer:
#include <stdio.h>
#include<string.h>
int main() {
int n, i,len,flag;
char str[1000]; //数组不要太小
while (scanf("%d", &n)!=EOF) {
getchar();
while(n--){
gets(str);
len=strlen(str);
flag=0;
for(i=0;i<len;i++){
if(str[i]<0){ //一个汉字占两个字节,每个字节表示一个负数
flag++; //因此统计负数个数,再除以2即为汉字个数
}
}
printf("%d\n",flag/2); //注意换行!
}
}
return 0;
}