-1

this my code when i give the input of aeiou AEIOU it only out puts xxxxx where did i go wrong??? i want to replace every vowel in the entire string but i only does it for the first word is my code wrong ??

#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);                                                 
maniteja
  • 687
  • 2
  • 16
  • 32

4 Answers4

4

cin>>input_string; will extract text from the input until it reaches whitespace, so only the first word of your input will be put into input_string. Use std::getline in the string library to get a whole line.

James Reed
  • 491
  • 3
  • 9
2

Use std::getline instead of cin>>input_string

cin>>input_sting will only read input string till space.

std::getline( std::cin, input_string); 
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
1

Just like scanf, cin ignores whitespaces. Check this out. Try gets http://www.cplusplus.com/reference/cstdio/gets/ in order to read the string.

EDIT: As @James Reed pointed out, gets is getting removed from the standard. Another answer has provided an alternative. I'm leaving the answer though for future reference.

Community
  • 1
  • 1
rhobincu
  • 906
  • 1
  • 7
  • 22
0

You need to use std::getline, here's a cleaner / simpler c++11 implementation.

#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;
}
OneOfOne
  • 95,033
  • 20
  • 184
  • 185