Help with removing vowels?

Write your question here.
i am suppose to right a loop that finds and remove the vowels of a sting i cannot make it work. My program only removes the first letter. i have not added all the vowels yet i wanted to get it to work with just an 'a' first .
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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

string removeVowel(string);

int main()
{
	string word;
	string SENTINEL = "app";

	do
	{
		cout << "Get the word:";
		cin >> word;
		cout << endl;
		removeVowel(word);
	} while (word != SENTINEL);


	return 0;
}
string removeVowel(string word)
{



	int length = word.length();

	for (int counter = 0; length > counter; counter++)
	{
		if (word.at(counter) == 'a')
		{
			word.erase(counter, 1);
			cout << word;
		}
	}
	return 0;
}
Instead of removing a vowel you can copy the non-vowels to a new string and return it.

1
2
3
4
5
6
7
8
9
10
11
12
string removeVowel (string word)
{
  string retval; // creates empty string
  for (char ch : word)
  {
    if (ch != 'a') // check for all vowels later
    {
      retval += ch; 
    }
  }
  return retval;
}
See:

http://www.cplusplus.com/reference/string/string/find_first_of/

1
2
3
4
5
6
  std::size_t pos;
  while((pos = word.find_first_of("aeiou")) != std::string::npos)
  {
			word.erase(pos, 1);
			cout << word;
  }
The function removeVowel() in the original code is problematic.
Example:
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
#include <iostream>
#include <string>

using namespace std;

string removeVowel(string word)
{
    int length = word.length();

    for (int counter = 0; length > counter; counter++)
    {
        if (word.at(counter) == 'a')
        {
            word.erase(counter, 1);
            cout << word;
        }
    }
    return 0;
}

int main()
{
    string fruit  = "banana";
    
    cout << removeVowel(fruit); 
}

Output:
bnanabnnabnnterminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::at

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Several issues:
• after using the erase() function, the length of the string will change, but the variable int length contains the original unchanged value.
• the function promised to return a string but instead returns zero.
• there is a cout (presumably for debugging) which clutters the result.

This might have been better:
1
2
3
4
5
6
7
8
9
10
11
string removeVowel(string word)
{
    for (unsigned int counter = 0; word.length() > counter; counter++)
    {
        if (word.at(counter) == 'a')
        {
            word.erase(counter, 1);
        }
    }
    return word;
}


This might have been better:
The problem is line 7. When erased the counter will skip one position.
Good point. I missed that one.
(focused first on getting it to not crash).
Last edited on
Topic archived. No new replies allowed.