知识库

C++语言开发示例:18. 字符

2025-02-01 20:29:06 李腾 1 次阅读

cctypes.cpp

#include <iostream>
#include <cctype>

int main()
{
    using namespace std;
    cout << "Enter text for analysis, and type @"
            "to terminate input.\n";
    char ch;
    int whitespace = 0;
    int digits = 0;
    int chars = 0;
    int punct = 0;
    int others = 0;

    cin.get(ch);
    while (ch != '@')
    {
        if (isalpha(ch))
            chars++;
        else if (isspace(ch))
            whitespace++;
        else if (isdigit(ch))
            digits++;
        else if (ispunct(ch))
            punct++;
        else
            others++;
        cin.get(ch);
    }
    cout << chars << " letters, "
         << whitespace << " whitespace, "
         << digits << " digits, "
         << punct << " punctuations, "
         << others << " others.\n";
}
转载请注明转自www.hylab.cn,原文地址:C++语言开发示例:18. 字符

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!