本文主要是介绍cctype字符函数库使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <iostream>
#include <cctype> //字符函数原型头文件
using namespace std;int main()
{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); // get first character 这样处理不会忽略空格、制表符、换行符while (ch != '#') // test for sentinel{if(isalpha(ch)) // 字符chars++;else if(isspace(ch)) // 标准空白字符,如空格、进纸、换行符、回车、水平制表符或者垂直制表符whitespace++;else if(isdigit(ch)) // 数字(0~9)digits++;else if(ispunct(ch)) // 标点符号punct++;elseothers++;cin.get(ch); // get next character}cout << chars << " letters, "<< whitespace << " whitespace, "<< digits << " digits, "<< punct << " punctuations, "<< others << " others.\n";return 0;
}
这篇关于cctype字符函数库使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!