#include <iostream>
#include <string.h>
using namespace std;

int main()
{
         char test[256];
         int x,i;
         int vowels;
         cout<<"please enter a word";
         gets(test);

         x = strlen(test);

         for(i=0;i<=x-1;i++)
         {
                   if(test[i]!=' ')
         {
         cout<<test[i]<<endl;
         }          
        if (test[i]=='a' || test[i]=='e' || test[i]=='i' || test[i]=='o' || test[i]=='u' || test[i]=='A' || test[i]=='E' || test[i]=='I' || test[i]=='O' || test[i]=='U') {
            test[i]=' ';


         }
         }


         cout<<"remove spaces % vowels =" << test << endl;



         system("pause");
         return 0;

}

Hi guys,
I'm new here to Daniweb, I'm also new to the world of programming and hopefully you can help me out with some good pointers and advice. I've started the program to enter a string or sentence and remove the spaces. next i wanted to remove the vowels which i found ok. but my problem is removing vowels and spaces at once. cheers guys and gals.

Recommended Answers

All 6 Replies

If you are writing a c++ program why are you using c's gets() function instead of c++ cin or getline()?

>>if (test=='a' || test=='e' || test=='i' || test=='o' || test=='u' || test=='A' || test=='E' || test=='I' || test=='O' || test=='U') {
test=' ';


Probably easier to use a switch statement

switch( tolower(test[i]) )
{
   case 'a': case 'e': case 'i': case 'o': case 'u': case ' ': case '\t':
       // do somethking
       break;
}

Cheers for the input, why I'm using gets()is to call my string(sentence) for example " I want to go to the shop" it calls the full sentence.
cin>> only will call "I" one character from the whole sentence, this is why i use the gets() function. never used getline() before not sure about this. also guys my problem with the above program is after i remove vowels from string how to remove the spaces from the string were the vowels were i hope this explains better than before. Thanks again..

I think I understand now, you don't want to remove vowels and spaces, just the vowels. When you find a vowel just sift all the remaining characters left so that they overwrite the vowel. You can use either memmov(), strcpy() or a loop to do that. Just make sure not to try this with a string literal or a pointer to a string literal because they can not be changed and your program will most likely crash.

As fistPerson already said, if you use std::string then you just call it's erase() method.


As for getline(), its very simple:

char text[100];
cin.getline(text, sizeof(text));

I think I understand now, you don't want to remove vowels and spaces, just the vowels. When you find a vowel just sift all the remaining characters left so that they overwrite the vowel. You can use either memmov(), strcpy() or a loop to do that. Just make sure not to try this with a string literal or a pointer to a string literal because they can not be changed and your program will most likely crash.

As fistPerson already said, if you use std::string then you just call it's erase() method.


As for getline(), its very simple:

char text[100];
cin.getline(text, sizeof(text));

thanks for the input Ancient Dragron i get the cin.getline() now. and using strcpy()would i implement this the same as strlen for example w = strcpy(text), and also would i need to declare two new char[] array to deal with copying over the spaces. I,ve seen other examples on the net, put don't understand i find the code more advanced to what im doing at the moment. thanks again for your help.

>>w = strcpy(text),
No

>> would i need to declare two new char[] array
No, just use the same char array. Assume you have a for loop with i counter, and the character at text == a vowel you want to delete strcpy(&text[i], &text[i+1]);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.