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

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

#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;
}
bigege 回答:从句子中删除给定字符的C ++程序不起作用

您没有读完整的句子。您可以使用getline

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

Godbolt上直播

本文链接:https://www.f2er.com/3132821.html

大家都在问