将字符串的标点去掉并且输出剩余部分字符

首先,去掉标点符号这一步,需要我们能够识别标点符号。而处理string对象中的字符,的关键问题是如何获取字符本身,这就需要涉及到语言和库。

在cctype头文件中定义了一组标准库函数来处理这部分的工作。

函数名称返回值
isalnum()如果参数是字母数字,即字母或数字,该函数返回true
isalpha()如果参数是字母,该函数返回真
isblank()如果参数是空格或水平制表符,该函数返回true
iscntrl()如果参数是控制字符,该函数返回true
isdigit()如果参数是数字(0~9),该函数返回true
isgraph()如果参数是除空格之外的打印字符,该函数返回true
islower()如果参数是小写字母,该函数返回true
isprint()如果参数是打印字符(包括空格),该函数返回true
ispunct()如果参数是标点符号,该函数返回true
isspace()

如果参数是标准空白字符,如空格、进纸、换行符、回车

、水平制表符或者垂直制表符,该函数返回true

isupper()如果参数是大写字母,该函数返回true
isxdigit()如果参数是十六进制的数字,即0~9、a~f、A~F,该函数返回true
tolower()如果参数是大写字符,则返回其小写,否则返回该参数
toupper()如果参数是小写字母,则返回其大写,否则返回该参数
转自:http://blog.csdn.net/dingwood/article/details/7401146

这边需要函数ispunct();

如何找出标点并去除,可以用范围for逐步遍历字符串,从而输出非标点字符串

#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <cctype>
using namespace std;
int main(int argc, char** argv) {
	string s;
	cout << "请输入一个字符串,最好是含有某些标点符号:" << endl; 
	getline(cin,s);
	for(auto n : s)
	{
		if(!ispunct(n))
			cout << n;
	}
	cout << endl;	
				
	
	return 0;
}
遇到问题:

范围for的书写格式 for(auto n s)中间是冒号 

cout<<end;可以放到遍历外面,不然每次遍历一个字符便会输出回车。

这个问题也可以通过普通的for循环,用下表执行随机的访问,从而输出

#include <iostream>
#include <string>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
#include <cctype>
using namespace std;
int main(int argc, char** argv) {
	string s, result;
	cout << "请输入一个字符串,最好是含有某些标点符号:" << endl; 
	getline(cin,s);
	
	for(int n = 0; n < s.size(); n++)
	{
		if(!ispunct(s[n]))
		{
			result += s[n];
		}
	}
	cout << result << endl; 
		
	return 0;
}

当然要记得getline(cin,s)是读取整行并且遇到回车符结束。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值