C++从句子中删除给定字符的程序不起作用

C++ Program to Remove given Character from the sentence doesn't work

本文关键字:字符 程序 不起作用 句子 删除 C++      更新时间:2023-10-16

程序只是从第一个单词中删除字符,而不是从所有句子中删除字符。我该怎么修?我需要删除句子中的所有字符,而不仅仅是第一世界。谢谢

#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
char c;
std::cout << "Enter the string : ";
std::cin >> s;
std::cout << "nEnter the character : ";
std::cin >> c;
/* Removing character c from s */
s.erase(std::remove(s.begin(), s.end(), c), s.end());
std::cout << "nString after removing the character "
<< c << " : " << s;
}

你没有阅读整句话。您可以使用getline:

std::cout << "Enter the string : ";
std::getline(std::cin, s);

Godbolt 直播