Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to write a function in c to remove all vowels from a given string and then move all letters to the left so that there are o empty places when a vowel is removed. an example of how this function should work is that if the string input is 'hello', the program should return 'hll'(with no empty spaces where the vowels were)
note that this is a function so the function defintion should be:
void removeVowels(char* word)
your help is much appreciated. thanks :)

What I have tried:

thats what i have done but its wrong:
C++
void removeVowels(char* word)
{
	char word[100],newString[100];
	int i, j = 0;
	for (i = 0; i <= strlen(word); i++) {
		if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u') {
			word[i] = ' ';
		}
		else {
			newString[j++] = word[i];
			newString[j] = '\0';
	}
Posted
Updated 14-Oct-16 22:51pm
v3
Comments
Richard MacCutchan 15-Oct-16 4:48am    
The function needs to return the new string, so its definition should include a char* Return type. Also, you do not need to replace a vowel by a space, but just do not copy it to the new string.
Member 12794813 15-Oct-16 4:51am    
Thanks for the response,Richard
i had return initially but it wouldn't compile. i thought void should not have a return statement but i am a beginner in programming so i am not really sure.
do you mind fixing what is wrong with my code please?
Richard MacCutchan 15-Oct-16 7:10am    
You need to change the definition to
char* removeVowels(char* source)
Then you need to allocate a new character array, using malloc(), inside the function, where you store all the other characters. Once you have finished, just return the pointer to the allocated buffer.

1 solution

One of the reasons your code don't work is because you use word as a parameter and as local variable, which is a never do.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900