首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编写一个程序,纠正字符串中的一个额外字符。

编写一个程序,纠正字符串中的一个额外字符。
EN

Stack Overflow用户
提问于 2020-12-19 06:20:02
回答 1查看 66关注 0票数 0

编写一个C++程序来纠正字符串中的一个额外字符。

例如:

输入

也是听音乐的好时光

输出

听音乐的好时机

我是C++的新手,不知道如何做到这一点。有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-19 06:48:50

在C++中,可以使用字符串库来执行字符串操作。这里我使用了字符串库的两种方法来回答您的问题。

方法1:使用replace()

代码语言:javascript
复制
string sentence = "Excellent time too listen to music";
sentence.replace(15, 3 , "to"); // replaces string found in 15th position, 3 places, with the string to 
cout << "Using str.replace method : " << sentence << endl;

方法2:使用erase()

代码语言:javascript
复制
string sentence = "Excellent time too listen to music";
sentence.erase(17,1); // erases the 17th character, and erase 1
cout << "Using str.erase method : " << sentence << endl;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65367228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档