编写一个C++程序来纠正字符串中的一个额外字符。
例如:
输入
也是听音乐的好时光
输出
听音乐的好时机
我是C++的新手,不知道如何做到这一点。有人能帮忙吗?
发布于 2020-12-19 06:48:50
在C++中,可以使用字符串库来执行字符串操作。这里我使用了字符串库的两种方法来回答您的问题。
方法1:使用replace()
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()
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;https://stackoverflow.com/questions/65367228
复制相似问题