且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C语言之统计输入字符数量

更新时间:2022-01-23 06:36:49

这个程序市委了统计所输入的数字或者英文字母的数字的数量,当然稍加改动便可以统计特殊字符的个数,在此不再冗叙。
代码如下:

#include <iostream>

using namespace std;

int main()
{
     char ch;
     int numberInt=0,numberChar=0;
    cout<<"Please input character \n"<<endl;
    cin>>ch;
    numberInt++;
    numberChar++;
    while(ch!='#'){
        if(ch>'0'&&ch<'9'){
            ch=ch-'0'+1;
            cout<<ch<<"Not a character,Please input corrected character\n"<<endl;
            cin>>ch;
            numberInt++;
        }else{
            cout<<"You really input the character\t"<<ch<<endl;
            cin>>ch;
            numberChar++;
        }
    }
    cout<<"code end !"<<endl;
    cout<<"you type numbers for %d numbers"<<numberInt<<endl;
    cout<<"you type character for %d numbers"<<numberChar<<endl;
    return 0;
}