这里一个简单的例子,查找出字符串中有几个标点符号。
代码如下:
#include <string>
using std::string;
#include <cctype>
using std::isupper;
using std::toupper;
using std::islower;
using std::tolower;
using std::isalpha;
using std::isspace;
#include <iostream>
using std::cout;
using std::endl;
int main()
{
string s("Hello world!!!");
string::size_type punct_cnt = 0;
for(string::size_type index = 0; index != s.size(); ++index)
if(ispunct(s[index]))
++punct_cnt;
cout << punct_cnt << " punctuation characters in " << s << endl;
return 0;
}
运行截图: