removing characters from a string

Apr 23, 2014 at 8:18am
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??

Apr 23, 2014 at 8:24am
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 .
Apr 26, 2014 at 12:39pm
Still I could not solve the problem. I did not understand how can I use the erase function.
Apr 26, 2014 at 1:19pm



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;
}
Apr 26, 2014 at 2:49pm
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.