Removing vowels from a string

So the goal here is to take a string, and remove all of its vowels (not including y) So mine works for all vowels except 'a' and 'u' and I have no idea why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string disemvowel(std::string str)
{
  for (int i = 0; i < str.length(); i++)
  {
    if (str.at(i) == 'a' || str.at(i) == 'u' ||str.at(i) == 'e' 
    || str.at(i) == 'i' || str.at(i) == 'o' )
    {
      str.erase(i,1);
    }  
    else if (str.at(i) == 'A' || str.at(i) == 'E' || str.at(i) == 'I' || 
    str.at(i) == 'O' || str.at(i) == 'U')
    {
      str.erase(i,1);
    }
   }
    return str;
}
Your little problem is ... i++ ... following str.erase(i,1);

You will jump over, without looking at, any vowel following directly after another that you erased. There is nothing special about 'a' or 'u' - I expect that just corresponds to your test case.

You can google "erase-remove idiom" (or, here, erase-remove if idiom).

Alternatively, you could just build another string up character-wise from the characters of the first string that weren't vowels. Erasing one at a time (and consequently shifting all the other characters forward) isn't a very efficient operation.
Last edited on
Hi, man. I have run your function in this program and I received the expected output.

Code:

1
2
3
4
  std::string str("abcdefghij...nopqrstuv  ABCEFGHIJ...NOPQRSTUV");
  std::cout << str << std::endl;
  std::string remove = disemvowel(str);
  std::cout << remove << std::endl;


Output:

abcdefghij...nopqrstuv  ABCEFGHIJ...NOPQRSTUV
bcdfghj...npqrstv  BCFGHJ...NPQRSTV


PD: I recommend you search about the function "first" for strings. Your code will be shorter and easier.
@dfortizc,
Try your code with that ancient advertising classic: "Beans means Heinz". You will see the problem.
Thank you guys!

@lastchance thanks for the reference. Showed me a much simpler way to do this!

1
2
3
4
5
6
7
8
9
10
11
    str.erase(std::remove( str.begin(), str.end(), 'a' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'u' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'o' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'e' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'i' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'A' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'U' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'O' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'E' ), str.end() );
    str.erase(std::remove( str.begin(), str.end(), 'I' ), str.end() );
    return str;
for x in "aeiouAEIOU":

just build another string
is simpler
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <regex>

static const std::string vowels = "aeiouAEIOU" ;

bool is_vowel( char c ) { return vowels.find(c) != std::string::npos ; }

std::string remove_vowels( const std::string& str )
{
    std::string result ;
    for( char c : str ) if( !is_vowel(c) ) result += c ;
    return result ;
}

std::string remove_vowels2( std::string str )
{
    str.erase( std::remove_if( str.begin(), str.end(), is_vowel ), str.end() ) ;
    return str ;
}

std::string remove_vowels3( std::string str )
{
    std::string result ;
    std::remove_copy_if( str.begin(), str.end(), std::back_inserter(result), is_vowel ) ;
    return result ;
}

std::string remove_vowels4( const std::string& str )
{
    static const std::regex vowels_re( '[' + vowels + ']' ) ;

    return std::regex_replace( str, vowels_re, "" ) ;
}

int main()
{
    const std::string str = "Remove all vowels (ie. [aeiouAEIOU]) from this string!" ;
    std::cout << str << '\n'
              << remove_vowels(str) << '\n'
              << remove_vowels2(str) << '\n'
              << remove_vowels3(str) << '\n'
              << remove_vowels4(str) << '\n' ;
}

http://coliru.stacked-crooked.com/a/69783b77d5787a82
Topic archived. No new replies allowed.