Not sure how to start this, using substr

I have no idea where to start, this is what the assignment says:

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 sting. For example, if str = "There" then after removing all the vowels, str = "The". 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.

Can you call another function when using substr? I'm not sure how to do this?

This is all I have so far.. I know this has to be simple, but I just can't wrap my head around how to go about doing this.

Thanks in advance,
Brooke! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 #include <iostream>
#include <string>

using namespace std;

int isVowel(string);
int removVowel(string);

int main()
{
    string str;
    cout << "Enter a string:" << endl;
    cin>> str;
    
    
}
Here I can suggest you the steps:-

1. Take any string as a input from user.
2. call function RemoveVowels()
prototype of function should be:-
 
string RemoveVowels(string inputString);

This function returns you final substring after removing all vowels in given input string.
3. In this function you have to call another function who tell you whether character in string is vowel or not?
prototype should be:-
 
bool IsVowel(char );
This will help you:-

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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

bool IsVowel(char InChar)
{
	if(InChar=='a'||InChar=='A'||InChar=='e'||InChar=='E'||InChar=='i'||InChar=='I'||InChar=='o'||InChar=='O'||InChar=='u'||InChar=='U')
	{
		return true;
	}
	else
		return false;
}

string RemoveVowels(string szstr)
{
	char* szSub = new char[szstr.size()+1];
	
	int j =0;
	for(int i =0;i<szstr.size();i++)
	{
		if(!IsVowel(szstr[i]))
		{
			szSub[j] = szstr[i];

			j++;
		}

	}
	szSub[j] = '\0';
	return szSub;
}

int _tmain(int argc, _TCHAR* argv[])
{
	string szInputString;

	cout<<"\n Enter string : - ";
	cin>>szInputString;
	cout<<"\nSubString "<<RemoveVowels(szInputString);
	getch();

	return 0;
};
My problem is that I have to use substr and I don't know how to implement it?
Use string::find_first_of to find each occurrence of a vowel.
Then use string::substr to get that part of the string and append it to your final result.
http://www.cplusplus.com/reference/string/string/find_first_of/

This will give you up to the first vowel:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstddef>

int main()
{
    std::cout << "Enter some text: ";
    std::string str;
    std::cin >> str; // Only one word, but whatever
    std::string result;
    
    // This just goes up to the first vowel
    std::size_t firstVowelPos = str.find_first_of("aeiouAEIOU");
    result += str.substr(0, firstVowelPos);
    
    std::cout << result;
}
Enter some text: throw
thr

To get everything else, you'll have to make lines 13-14 into a loop somehow. (Hint: keep track of the position of the last vowel that you found, so you know which position to start from for find_first_of and substr.)
Last edited on
Okay this is what I have so far.. Thanks for everyone's help!!
I still have two problems..
1. It is only reading the first word of my string, I've tried using con.getline and it isn't letting me.
2. I don't know how to change what I already have to use substr (since that was a requirement of the assignment).
Last edited on
To read more than one word, use std::getline(std::cin, myString);.

And...did you forget to copy/paste your code?
Anyways, I gave you an example using substr to get the first part of the string (up to the first vowel).
You just need to find a way to loop through the rest of the string and repeat.
Sorry, here's my code, thanks for the help with getline, it worked, but im not sure your code would work with mine?? WIll you check it out?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <string>

using namespace std;
string RemoveVowels(string);
bool IsVowel(char);


int main()
{
string szInputString;

cout<<"Enter a string: " << endl;
getline(cin, szInputString);
cout<< "SubString " <<RemoveVowels(szInputString);

return 0;
};

bool IsVowel(char charVar)
{

switch(charVar)
{
	case 'A':
	case 'a':
	case 'E':
	case 'e':
	case 'I':
	case 'i':
	case 'O':
	case 'o':
	case 'U':
	case 'u':
	case 'Y':
	case 'y':
	return true;
	
	default:
	return false;
	
}

}

string RemoveVowels(string szstr)
{
	char* szSub = new char[szstr.size()+1];

	int j =0;
	for(int i =0;i<szstr.size();i++)
	{
		if(!IsVowel(szstr[i]))
		{
			szSub[j] = szstr[i];

			j++;
		}

	}
	
	szSub[j] = '\0';
	
	return szSub;
}
Topic archived. No new replies allowed.