removing characters from a string

I wanna delete the vowel character from my string.. like if the input is apple the output should be ppl, another input for example saad the output should be sd.
how can I do that??

You could try std::string.erase http://www.cplusplus.com/reference/string/string/erase/

Alternatively you could create a new string with only the consonants .
Still I could not solve the problem. I did not understand how can I use the erase function.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

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

int main()
{

	string myString = "This is a simple string.";

	// 1st param is position in string.
	// sencond is how many chars to remove

	// 1st letter is at 0
	myString.erase(0, 5);	// remove "This "
	cout << myString;

	return 0;
}
Use remove_copy_if() to do the dirty work:

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

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

int main( int argc, char** argv )
{
  string s = argv[ 1 ];

  if (!s.empty())
    s.erase( remove_copy_if( s.begin(), s.end(), s.begin(), isvowel ), s.end() );
        
  cout << s << "\n";
}
D:\prog\foo> a "Hello world!"
Hll wrld!

D:\prog\foo> _

Hope this helps.
Topic archived. No new replies allowed.