【问题标题】:how to replace every match of a charecter in the entire string?? in c++如何替换整个字符串中一个字符的每个匹配项?在 C++ 中
【发布时间】:2014-01-28 08:30:49
【问题描述】:

当我输入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);                                                 

【问题讨论】:

  • 打印input_string会看到什么?
  • 您真的需要将 Emacs 模式行放在问题中吗?

标签: c++


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    使用std::getline 而不是cin&gt;&gt;input_string

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

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

    【讨论】:

      【解决方案3】:

      就像scanfcin 忽略空格。检查this out。尝试gets http://www.cplusplus.com/reference/cstdio/gets/ 以读取字符串。

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

      【讨论】:

      • gets 自 C++11 起已弃用。无论如何,除非你必须这样做,否则你不应该在 C++ 中使用 C 字符串。
      【解决方案4】:

      你需要使用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;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多