/*
* Copyright (c) 2012, 烟台大学计算机学院
* All rights reserved.
* 作 者:解晓东
* 完成日期:2012 年 11 月 17 日
* 版 本 号:v1.0
*
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:
* 算法设计:
*/
# include <iostream>
# include <string>//声明string类型
# include <cctype>//string类型变量的一些函数
using namespace std;
/*函数声明*/
int alphabet(string); //计算字母个数
int number(string); //计算数字个位数
int punctuation(string);//计算英文标点个数
int main()
{
/*变量的定义*/
string str1;
int ialphabet = 0;
int inumber = 0;
int ipunctuation = 0;
/*输入与输出*/
cout << "please input strings:";
cin >> str1;
/*函数的调用及赋值*/
ialphabet = alphabet(str1);
inumber = number(str1);
ipunctuation = punctuation(str1);
/*输出*/
cout << ialphabet
<< " of alphabets,"
<< inumber
<< " of numbers,"
<< ipunctuation
<< " of punctuations.\n";
return 0;//main函数结束
}
int alphabet(string str)/*函数的定义*/
{
int num = 0;
for (string::size_type count = 0; count != str.size(); ++count)
{
if (isalpha(str[count]))//isalpha()函数:如果是字母,返回true,否则false
{
++num;
}
}
return num;
}
int number(string str)
{
int num = 0;
for (string::size_type count = 0; count != str.size(); ++count)
{
if (isdigit(str[count]))//isdigit()函数:如果是数字,返回true,否则false
{
++num;
}
}
return num;
}
int punctuation(string str)
{
int num = 0;
for (string::size_type count = 0; count != str.size(); ++count)
{
if (ispunct(str[count]))//ispunct()函数:如果是英文标点,返回true,否则false
{
++num;
}
}
return num;
}
字符串分类,输出字符串中字母,数字,符号的个数
最新推荐文章于 2022-11-17 12:04:06 发布