Remove Vowels

Just needing pointers on figuring this out

1
2
3
4
5
6
7
Instructions
Write a program that prompts the user to input a string. 
The program then uses the function substr to remove all the vowels from the string. 
For example, if str = "There", then after removing all the vowels, str = "Thr". 
After removing all the vowels, output the string. 
Your program must contain a function to remove all the vowels and a function to determine 
whether a character is a vowel.


I see substr takes (position, size)
so I'm assuming there is no way to actually put into substr to take out certain values but will have to write the string a certain way to equal the same


my current bones code looks like

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
47
48
49
#include "pch.h"
#include <iostream>
#include <cctype>
#include <cmath>
#include <string>
using namespace std;

bool check_vowels(char ch)
bool remove_vowels()

//check if character is a vowel
bool check_vowels(char ch)
{
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
	{
		return true;
	}
	else
	{
		return false;
	}
}

//Remove vowels with substr
bool remove_vowels()
{
	return substr(a, e, i, o, u, A, E, I, O, U);
}

//Main
int main()
{
string mystring; 

    //input
cout << "Enter String";
getline(cin, mystring);
    
    //check
const bool check_v = check_vowels();
    
    //remove    
const bool remove_v = remove_vowels();
    
    //output
cout << "New string is = " << mystring; 
    
	return 0;
}
Last edited on
You should be able to do this without pointers. [That was a joke.]

"The program then uses the function substr to remove all the vowels from the string."
Where are you defining this function "substr" that you're calling? Not the clearest instructions, but I assume it's talking about the member function of std::string called "substr", and not your own function.
http://www.cplusplus.com/reference/string/string/substr/
I don't actually see how using substr is useful here.

Your program must contain a function to remove all the vowels
I don't understand signatures of the functions you're using. You're not even passing anything into your check_vowels function. To me, the instructions mean that you should write a function that takes in a string, and returns another string with the vowels removed. Or the string should be passed by reference. But either way, you need to pass a string as a parameter. Just like how you pass char ch as a parameter to check_vowels, you need to pass the string into your "remove vowels" function.

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
#include <iostream>
#include <cctype> // tolower
#include <string>

/// check if character is a vowel
bool isvowel(char ch)
{
    ch = std::tolower(ch);
	return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}

/// Remove vowels
std::string remove_vowels(const std::string& str)
{
	std::string str_out;
	for (size_t i = 0; i < str.size(); i++)
	{
		if (!isvowel(str[i]))
		{
			str_out += str[i];   
		}
	}
	return str_out;
}

int main()
{
	using std::cout;
    
	std::string mystring; 
	cout << "Enter String: ";
	std::getline(std::cin, mystring);

	mystring = remove_vowels(mystring);

	cout << "New string is = " << mystring << '\n';
        
	return 0;
}


Now, if you still need to incorporate substr, read the link I pasted above, and contrive some way to use it (personally I don't think it's helpful).
Last edited on
The coding you gave me passes the check
but for learning sake i wanna learn how to use the substr if possible.
reading the link above tryen to figure out how to use the substr to pull out the vowels it comes across
i did change the abc code you had though, its cleaner
From:
1
2
3
4
5
6
7
8
9
10
11
12
//check if character is a vowel
bool check_vowels(char ch)
{
	if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
	{
		return true;
	}
	else
	{
		return false;
	}
}


to:
1
2
3
4
5
6
//check if character is a vowel
bool check_vowels(char ch)
{
    tolower(ch);
    return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
}
Last edited on
tolower(ch) isn't mutating the character passed in, unless that's a custom function you made. Try testing your program with capital vowels.

I honestly am not sure how I would use substr here. I don't know what the teacher is looking for. Maybe someone else has a better idea.
Last edited on
Hello blackstar,

Before you can use "substr" to get what is not a vowel you need to the position of the vowels in the the string.

Take a look at http://www.cplusplus.com/reference/string/string/find_first_of/ the example at the bottom of the page should be helpful. Knowing the position of the vowels you can use "substr" to build a new string.

Hope that helps,

Andy

Sadly, I can't think of any remotely sensible way to use std::string.substr() to remove all the vowels. Are you sure that you have read your assignment correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isvowel( char c )
{
   const string vowels = "aeiouAEIOU";
   return vowels.find( c ) != string::npos;
}

string substr( string str )
{
   str.erase( remove_if( str.begin(), str.end(), isvowel ), str.end() );
   return str;
}

int main()
{
   string str;
   cout << "Input a test string: ";   getline( cin, str );
   cout << substr( str ) << '\n';
}


Last edited on
yeah it wants us to use the function substr() to remove all the vowels
http://www.cplusplus.com/reference/string/string/substr/
a few comments here work, but trying to change them to use the substr seems to be a headache.

handy andy seems to have to closet result, find the positioning then remove it with the substr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cctype>
#include <cmath>
#include <string>
#include <cstddef>
using namespace std;

int main ()
{
    string mystr;
    cout << "Enter String ";
    getline(cin, mystr);
    string str (mystr);
    size_t found = str.find_first_of("aeiou");
    while (found!=string::npos)
    {
        str[found]='*';
        found=str.find_first_of("aeiou",found+1);
    }
    cout << str << '\n';
    return 0;
}


will find and replace, now just got to find and remove
Take this example here http://www.cplusplus.com/reference/string/string/find_first_of/
and instead of '*' use as replace string '\0'. To abuse substr() for this task is feasible, a nice exercise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string mystr;
    cout << "Enter String ";
    getline(cin, mystr);
    string str;  
    size_t start = 0;
    size_t end = 0;
    
    while (true)
    {
        end = mystr.find_first_of("aeiouAEIOU",start);
        if (end == string::npos) break;
        str += mystr.substr(start, end-start);
        start = end+1;
    }
    str += mystr.substr(start, mystr.length()-start);
    cout << str << '\n';
    return 0;
}
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
#include <iostream>
#include <string>
using namespace std;

bool isvowel( char c )
{
   const string vowels = "aeiouAEIOU";
   return vowels.find( c ) != string::npos;
}

string noVowels( string str )
{
   for ( int i = 0; i < str.size(); i++ )
   {
      if ( isvowel( str[i] ) ) return str.substr( 0, i ) + noVowels( str.substr( i + 1 ) );
   }
   return ""; 
}

int main()
{
   string str;
   cout << "Input a test string: ";   getline( cin, str );
   cout << noVowels( str ) << '\n';
}
Last edited on
Hello blackstar,

BTW do not edit your original post when you make changes. It just confuses people that see the first post and do not realize that you have changed it. It is better to make a new message to show your changes.

After rearranging the instructions:

Instructions

Write a program that prompts the user to input a string.

The program then uses the function substr to remove all the vowels from the string.

For example, if str = "There", then after removing all the vowels, str = "Thr".

After removing all the vowels, output the string.

Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel.


Here is the confusing part. Does "function substr" mean a function that you write or the member function of the string class called "substr"?

Now that I have reread everything a couple of times I understand better.

The page I pointed you to was to see how "find_first_of" works. Not so much to copy it and try to use it.

Using the return value of "find_first_of" and "myString.substr()" you can create a new string with out the vowels.

I do believe that is the intent of the instructions.

Hope that helps,

Andy
Oh i totally understand Handy andy, i just liked how simple their post was and decided to try and modify it.
As for the edits, it was more editing to read better but not modify the original words to the original post.
my understanding of things works better when its simple and slowly adding in more complicated function.

I also liked mikeStgt post, the \0 worked beautifully and kept the original coding intact and simple. even though we weren't able to get the substr in

MikeStgt (126)
Take this example here http://www.cplusplus.com/reference/string/string/find_first_of/
and instead of '*' use as replace string '\0'. To abuse substr() for this task is feasible, a nice exercise.


still reading over the other post as well
and i want to thank you all for the coding ideas though future reference next to the coding if you can add (//information about the code and why) so i can understand why it has been done

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream> 
#include <cctype>
#include <cmath>
#include <string>
#include <cstddef>
using namespace std;

int main ()
{                                                   // just basic fast example
    string mystr;                                  //custom string declaration (named mystr)
    cout << "Enter String ";                      //see out "Enter String"
    getline(cin, mystr);                         //getting line (to see in custom string mystr)
    string str (mystr);                          
    size_t found = str.find_first_of("aeiou");
    while (found!=string::npos)
    {
        str[found]='\0';
        found=str.find_first_of("aeiou",found+1);
    }
    cout << str << '\n';
    return 0;
}


But once again thank you all for your help
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string str;
    cout << "Enter String: ";
    getline (cin, str);

    size_t pos{0};
    while ((pos = str.find_first_of ("aeiouAEIOU", pos)) != string::npos)
    {
        str.erase (pos, 1);
    }

    cout << str << '\n';
    return 0;
}
Last edited on
My idea how to abuse substr() for this task was, concatenate the substring before the vowels with the substring after it, skipping this way over the vovel in question. I coded it last nite but did not publish it. (Honestly, it was late and I was too lazy to debug find(), it did not work as I assumed from REXX.)
Topic archived. No new replies allowed.