首页 > 解决方案 > 从句子中删除给定字符的 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;
}

标签: c++char

解决方案


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

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

住在神螺栓上


推荐阅读