1. 程式人生 > >漢字統計

漢字統計

漢字在字串中以兩個負數的形式儲存,所以只要算出負數個數,除以2就可以了
在這裡插入圖片描述

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=1000;
char str[maxn];
int main()
{
    int n;
    while(cin>>n){
            getchar();
            while(n--){
                gets
(str); int sum=strlen(str),cnt=0; for(int i=0;i<sum;i++) if(str[i]<0) cnt++; cout<<cnt/2<<"\n"; } } return 0; }