字符串分类,输出字符串中字母,数字,符号的个数

/*  
* 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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值