如何替换整个字符串中字符的每个匹配项?在 C++ 中

how to replace every match of a charecter in the entire string?? in c++

本文关键字:C++ 字符 替换 何替换 字符串      更新时间:2023-10-16

这是我的代码,当我给出输入时aeiou AEIOU它只出来xxxxx我哪里出错了??? 我想替换整个字符串中的每个元音,但我只对第一个单词这样做,我的代码是错误的吗?

#include<iostream>                                                                                                                                    
#include<string>                                                                                                                                      
#include<algorithm>                                                                                                                                   
using namespace std;                                                                                                                                  
class remove_vowels_class                                                                                                                             
{                                                                                                                                                     
 private:                                                                                                                                             
 public:                                                                                                                                              
    void remove_vowels_accept(string the_string)                                                                                                      
    {                                                                                                                                                   
      std::replace(the_string.begin(),the_string.end(),'a','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'A','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'e','x');                                                                                          
      std::replace(the_string.begin(),the_string.end(),'E','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'i','x');                                                                                        
      std::replace(the_string.begin(),the_string.end(),'I','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'o','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'O','x');                                                                                         
      std::replace(the_string.begin(),the_string.end(),'u','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'U','x');                                                                                      
      std::replace(the_string.begin(),the_string.end(),'~',' ');                                                                                      
      cout<<"the replaced string is :"<<the_string<<endl;                                                                                             
  }                                                                                                                                                   
};                                                                                                                                                    
int main()                                                                                                                                          
  {                                                                                                                                                   
remove_vowels_class maniobj;                                                                                                                      
string input_string;                                                                                                                              
cout<< "Enter a string to replace all vowels:"<<endl;                                                                                             
cin>>input_string;                                                                                                                                
std::replace(input_string.begin(),input_string.end(),' ','~');                                                                                    
maniobj.remove_vowels_accept(input_string);                                                 

cin>>input_string;将从输入中提取文本,直到它到达空格,因此只有输入的第一个单词会被放入input_string。使用字符串库中的std::getline获取整行。

使用 std::getline 而不是 cin>>input_string

cin>>input_sting只会读取输入字符串直到空格。

std::getline( std::cin, input_string); 
就像

scanf一样,cin忽略空格。看看这个。尝试gets http://www.cplusplus.com/reference/cstdio/gets/以读取字符串。

编辑:正如@James里德指出的那样,gets正在从标准中删除。另一个答案提供了另一种选择。不过,我将答案留待将来参考。

你需要使用 std::getline,这是一个更干净/更简单的 c++11 实现。

#include <string>
#include <iostream>
std::string replace_vowels(const std::string & str) {
    static const char vowels[] = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};
    std::string cp(str);
    for(std::size_t i = 0; i < str.length(); ++i) {
        auto & ch = str[i];
        for(auto & v : vowels) {
            if(ch == v) {
                cp[i] = 'x';
                break;
            }
        }
    }
    return std::move(cp);
}
int main() {
    std::string input, output;
    std::cout << "Enter a string to replace all vowels : ";
    std::getline(std::cin, input);
    output = replace_vowels(input);
    std::cout << "Input : " << input << " / Output : " << output << std::endl;
}