Can I use string::find and string::insert to insert ub before each vowerl character

Dec 7, 2015 at 5:04am
Hello,I really need help with making the translator

I need to add ub before each vowerl character and I am wondering if I could use
string::find and string::insert ?


Dec 7, 2015 at 5:07am
I need to add ub before each vowerl character and I am wondering if I could use
string::find and string::insert ?

Yes, you could (although you might find string::find_first_of to be a better fit.) Why don't you give it a shot?
Dec 7, 2015 at 5:12am
but I need to find not only the first character @cire
Dec 7, 2015 at 5:16am
but I need to find not only the first character @cire

So, write some code. See how it works out.
Dec 7, 2015 at 5:28am
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?
Dec 7, 2015 at 5:29am
hello, @cire

here is my code
1
2
3
4
5
6
7
8
9
string str (word);
  size_t found = str.find_first_of("aeiou");
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  return str;
Dec 7, 2015 at 5:42am
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.

-Albatross
Last edited on Dec 7, 2015 at 5:43am
Dec 7, 2015 at 5:47am
Except you actually caused some damage this time, before I replied. And I was really hoping that you wouldn't. But now, you're attacking newcomers.

-Albatross
Dec 7, 2015 at 5:55am
> 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 ;
}

http://rextester.com/ZIX91591
Last edited on Dec 7, 2015 at 6:00am
Dec 7, 2015 at 6:28am
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'.


#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 ;
}

http://rextester.com/ZIX91591


I am actually the owner of this post but there was some freaking disable my account..

so thanks @JLBorges (7248) it works on the website but im using codeblocs and for this part it didn't work on my program.

for( char this_char : str ) // for each character in the string
Last edited on Dec 7, 2015 at 7:09am
Dec 7, 2015 at 6:37am
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.

-Albatross
Dec 7, 2015 at 7:05am
hello @Albatross


is it like for for ( int i = 0; i < size; i++ ) ?
Last edited on Dec 7, 2015 at 7:05am
Dec 7, 2015 at 7:12am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ;
}

http://rextester.com/KYNM67475
Dec 7, 2015 at 7:25am
thank you it works! although I need to reorganize my whole switch statements code but thank you so much!
Dec 7, 2015 at 11:54am
you could also try -std=c++11 compilation flag to enable the C++11 features on your compiler.
http://stackoverflow.com/questions/18174988/how-can-i-add-c11-support-to-codeblocks-compiler
Topic archived. No new replies allowed.