C++ primer 第3章 ex3.10

本文介绍了一个简单的C++程序,用于接收用户输入并处理字符串,去除其中的标点符号。程序通过循环遍历输入字符串,判断每个字符是否为标点,如果是则忽略,否则添加到结果字符串中。最后,程序会根据用户输入的标点情况输出相应的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#pragma once
#include <string>
#include <iostream>
#include <cctype>
using namespace std;

void getstring()
{

	string result_str,userInput_str;
	bool hasPunct=false;
	cout<<"请输入字符传:"<<endl;
	cin>>userInput_str;
	for(string::size_type i=0;i<userInput_str.size();i++)
	{
		char check=userInput_str[i];
		if (ispunct(check))
		{
			hasPunct=true;
		}
		else
		{
			result_str += check;
		}

	}
	if (hasPunct)
	{
		cout<<"去标点后的字符串:"<<result_str<<endl;
	}else
	{
		cout<<"输入有误,输入必须包含标点!"<<endl;
	}

}

int main()
{
   
	while(true)
	{
	
		cout << endl << "1) 输出去标点后的字符串" << endl;
		cout << "2) 退出" << endl << endl;
		cout << "请选择 [1], [2] : ";
		string userInput; 
		
		getline(cin,userInput);
		
		if(userInput.size() == 0) continue;

		const char ch = userInput[0];

		if(ch == '2') break;
		else if(ch =='1') getstring();
	
		else cout << endl << "Input error. Enter 1, 2 and [Enter]."<< endl;
	}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值