-6

How can I replace vowel letters with a character, for example input: asd output:.sd, so we replaced vowel letters with '.', I tried to do that, but I couldn't don't know why, here is my code:

#include <iostream>
using namespace std;
int main () {
string s;
cin>>s;
if (s=="a")
    s='.';
if (s=="e")
    s='.';
if (s=="u")
    s='.';
if (s=="o")
    s='.';
if (s=="i")
    s='.';
  cout<<s<<endl;

}

why nothing is changed? input = output?

Adam Hussein
  • 75
  • 5
  • 12
  • @MEE I checked the answers, std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y' I don't understand this, I think this replaces only one vowel, won't replace all vowel letters, perhaps I don't understand this, can you teach me? – Adam Hussein Jan 21 '18 at 07:44

3 Answers3

3

You have a problem. When you read the string "s", it is equal to "asd". So when it is compared in the conditional (s=="a"), it is false. You must compare char by char. I share a possible solution:

#include <iostream>

int main()
{
    std::string s;
    std::cin >> s;
    for (char& c: s)
    {
        if (c == 'a' || c=='e' || c == 'i' || c == 'o' || c == 'u')
            c = '.';
    }
    std::cout << s << std::endl;
}
sigifredo
  • 152
  • 1
  • 8
3

You can use std::replace_if to replace a character if it's a vowel, using std::string::find to easily figure out if the char is a vowel i.e. :

std::string s = "abcvaoi";
std::replace_if( s.begin( ), s.end( ), [] ( const char c ) {
    return std::string( "aeiou" ).find( c ) != std::string::npos;
}, '.');
George
  • 2,101
  • 1
  • 13
  • 27
  • 1
    For implementations without a small string optimization, it may be worth replacing the `std::string` in the lambda with a plain array of char, and use `std::find`. – juanchopanza Jan 21 '18 at 08:03
-3

You may use switch case and for loop for simplicity.

using namespace std;
#include<iostream>
int main()
{
string a;
cin>>a;
for(int i=0;a[i]!='\0';i++)
{
    switch (a[i])
    {
        case 'a':a[i]='.';
                 break;
        case 'e':a[i]='.';
                 break;
        case 'i':a[i]='.';
                 break;
        case 'o':a[i]='.';
                 break;
        case 'u':a[i]='.';
                 break;


    }
}
cout<<a;
}
  • 4
    You can also combine all `case`s to one: `case 'a':\ncase 'e':\ncase 'i':\na[i]='.'` where \n means line break – MEE Jan 21 '18 at 07:57
  • 3
    Semi important note since a compiler won't make it obvious, `a[i]!='\0'` yields undefined behaviour pre C++0x, unless a nul byte is input manually. – George Jan 21 '18 at 08:00
  • Translating George's comment into plain English: The `string`s internal buffer is not guaranteed to be null-terminated until C++11. More here: https://stackoverflow.com/questions/11752705/does-stdstring-contain-null-terminator – user4581301 Jan 21 '18 at 08:06
  • @user4581301 I'm using codeblocks so I didn't get any problem regarding null character – Manojeet Das Jan 21 '18 at 08:10
  • That means nothing, unfortunately. Code::Blocks is a development environment, not a compiler. Put it on top of a compiler that elects not to null terminate, and to be honest most null terminate, and Code::Blocks will not save you. The problem is with portability. If the asker or some future reader uses this as the basis for an assignment and hands it in to a marker with a different set-up, they're flipping a coin. That said, until the asker announced they were targeting C++98, this wasn't an issue. Anyone teaching C++98 20 years later is... Kinda negligent, so I don't worry about it. – user4581301 Jan 21 '18 at 08:26
  • Indeed. I guess that is why the poster accepted this question, because he/she just wanted a full solution – QuIcKmAtHs Jan 21 '18 at 09:20