why you gotta be so rude @oscus1 ? Have I ever burnt your house or kill your family?
I am asking for help politely. You live in a cruel world. Do you know 'peace' ?
Do you know 'manner' ? Please calm down oscus1. I never bs about you so why ? Did you have a bad day?
The actions of osucs1 are not representative of the community here at cplusplus.com. Moving on!
Normally you'd pass found+1 as an argument to find_first_of to find each character in order, to start searching at the character after the one you found. However, you're inserting two characters before each vowel, right? Given that, what would you pass to find_first_of as the second argument instead of found+1?
EDIT: Okay, right. Need to talk to the site admin.
> I need to add ub before each vowerl character and I am wondering
> if I could use string::find and string::insert ?
It is perhaps easier (and cleaner) to create a new string with the vowels prefixed with 'ub'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <string>
bool is_one_of( char c, std::string char_set ) { return char_set.find(c) != std::string::npos ; }
std::string add_prefix( std::string str, std::string char_set, std::string prefix )
{
std::string result ;
for( char this_char : str ) // for each character in the string
{
if( is_one_of( this_char, char_set ) ) /* if it is a character in char_set */ result += prefix ; // add prefix
result += this_char ; // append the character
}
return result ;
}
for( char this_char : str ) // for each character in the string
{
if( is_one_of( this_char, char_set ) ) /* if it is a character in char_set */ result += prefix ; // add prefix
result += this_char ; // append the character
}
Ah, shame. That's a range-based for loop, which is a C++11 feature that doesn't seem to be enabled by default on many compilers.
You could do something along the lines of making a traditional for loop to iterate over the whole string (either using C++ iterators or a counter) instead, and have a variable in the loop named this_char that gets set each iteration. It'd require changing the for loop and adding an extra line of code near the top of it, but the rest should work fine.
std::string add_prefix( std::string str, std::string char_set, std::string prefix )
{
std::string result ;
for( std::size_t i = 0 ; i < str.size() ; ++i ) // for each character in the string
{
char this_char = str[i] ;if( is_one_of( this_char, char_set ) ) /* if it is a vowel */ result += prefix ; // add prefix
result += this_char ; // append the character
}
return result ;
}