天天看點

C++:統計字元串内大小寫字母,數字空格,逗号等

川川好久沒發文了,最近實在太忙,要是感興趣可以直接跟我私聊, QQ:2835809579

原題:

用賦初值的方法把字元串 “ C is a general purpose, procedural, imperative computer programming language developed in 1972 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system.” 存放到字元數組s中,程式設計統計其中的大寫字母、小寫字母、數字、空格、逗号的個數。

代碼:

#include<iostream>
using namespace std;

int main()

{
    char c;
    int smallletters = 0, largeletters=0,space = 0, digit = 0,douhao=0, other = 0;
    cout << "請輸入字元串:";
    while ((c = getchar()) != '\n')
    {
        if (c >= 'a' && c <= 'z')
        {
            smallletters++;
        }
        else if (c >= 'A' && c <= 'Z')
        {
            largeletters++;
        }
        else if (c == ' ')
        {
            space++;
        }
        else if (c >= '0' && c <= '9')
        {
            digit++;
        }
        else if (c == ',')
        {
            douhao++;
        }
        else
        {
            other++;
        }
    }
    cout <<"小寫字母:"<< smallletters <<"\t"<<"大寫字母:"<<largeletters<<"\t"<<"空格:"<< space <<"\t"<<"數字:"<< digit <<"\t"<<"逗号:"<<douhao<<"\t"<<"其它:"<< other <<"\t"<< endl;

    return 0;

}      

有問題,留言或者加我扣扣問我吧。

繼續閱讀