5 Replies - 5565 Views - Last Post: 26 October 2011 - 08:35 AM Rate Topic: -----

#1 yourfriendjoey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-July 11

Removing vowels from a string - C++

Posted 26 October 2011 - 07:18 AM

Hey everyone,

I'm having trouble completing this assignment and would like some tips on where I am going wrong.

The assignment comes at the end of a chapter on strings and enumeration types.

The instructions say to have the user input a word, and then return the word without vowels. Requires two functions, one to check if the letter is a vowel, and another to remove it.

Here's what I have:

#include <iostream>
#include <string>

using namespace std;

bool isVowel(string checkLetter);
string removeVowel(string word, int counter);

int main()
{
	string originalWord;
	int count = 0;
	string letter;
	char ch;

	cout << "Enter a word: ";
	cin >> originalWord;
	cout << endl;

	string newWord = originalWord;

	for (count = 0; count <= newWord.length(); count++)		//for each letter in the word, check if it's a vowel
	{
		letter = newWord.substr(count, 1);					//pull a letter
		if (isVowel(letter))								//if it's a vowel
			newWord = removeVowel(originalWord, count);					//pull it out of the word
	}

	cout << "The word, without vowels, is " << newWord << endl;

	return 0;
}


bool isVowel(string checkLetter)
{
	char ch;
	ch = static_cast<char>(checkLetter);

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

string removeVowel(string word, int counter)
{
	word = word.erase(counter, 1);

	return word;
}


Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Removing vowels from a string - C++

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Removing vowels from a string - C++

Posted 26 October 2011 - 07:51 AM

What are you having problems with? Are you receiving any errors?

What is the output you are getting, and what are you expecting?

This post has been edited by rgfirefly24: 26 October 2011 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 yourfriendjoey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-July 11

Re: Removing vowels from a string - C++

Posted 26 October 2011 - 07:59 AM

Sorry for not being more clear. I'm getting an error saying that I can't use a string in a switch structure.

When I try to convert the one character string into a char variable, it tells me I'm not allowed to do that.

So, I guess the main problem I have is I can't think of another way to remove the vowels. The ways I've thought up obviously are illegal.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 664
  • View blog
  • Posts: 2,277
  • Joined: 31-December 10

Re: Removing vowels from a string - C++

Posted 26 October 2011 - 08:27 AM

For your isVowel() function, you should change the parameter to a char then you can check it in a switch statement. It's unnecessary to have a 1 letter string, just use a char.

This post has been edited by vividexstance: 26 October 2011 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#5 yourfriendjoey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-July 11

Re: Removing vowels from a string - C++

Posted 26 October 2011 - 08:34 AM

View Postvividexstance, on 26 October 2011 - 08:27 AM, said:

For your isVowel() function, you should change the parameter to a char then you can check it in a switch statement. It's unnecessary to have a 1 letter string, just use a char.



The only problem with that is that I'm using
 newWord.substr(count, 1) 
to pull the character from the string. If I use a char variable to hold the letter, I can't use newWord.substr(count, 1). It can't turn a string into a char.

Unless I'm missing something?
Was This Post Helpful? 0
  • +
  • -

#6 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Removing vowels from a string - C++

Posted 26 October 2011 - 08:35 AM

Switch as far as I'm aware only works with Char and Int. How have you tried switching the string to char? What is the exact error your getting? All a string is is a char array if I remember right.

You could try this, although i'm sure there is a better solution:


	string originalWord;
	int count = 0;
	string letter;
	char ch;

	cout << "Enter a word: ";
	cin >> originalWord;
	cout << endl;

	const char* newWord = originalWord.c_str();

  for(count = 0;count <= (sizeof(newWord)/sizeof(newWord[0]));count++)
  {
		if (isVowel(newWord[0])
		{
			
		}
  }



You should then be able to change your isVowel to be isVowel(char letter)

Also you should be able to do something with string::findand string::at

string originalWord;
int count = 0;
string letter;
char ch;

cout << "Enter a word: ";
cin >> originalWord;
cout << endl;

string newWord = originalWord;

findVowels(newWord);


void findVowels(string word)
{
  string vowels = 'aeiouAEIOU'
	string Word = word;
	for(int i = 0;i< vowels.length;i++)
	{
		int position = Word.find(vowels.at(i),0);
		if (position > 0)
		{
		  Word = removeVowel(word,position);
		}
	}
}

string removeVowel(string word, int position)
{
	return word.erase(position);
}


This post has been edited by rgfirefly24: 26 October 2011 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1