how do i replace the 2nd occurrence of a letter?

hi guys, i`m currently racking my brains out over this issue. How do i go about the x as the first occurrence of the letter?

lets say helxo , x is the 2nd occurrence of l. I will want to change back x into l

i have replaced it with x in the earlier step with this code which was kindly given to me by a kind former vlad

1
2
3
4
5
6
7
8
9
10
string everySecondChar(const string &s,char source,char distance)
{
    string t(s);
    for(std::string::size_type even =0,pos=t.find(source,0);pos!=std::string::npos;pos=t.find(source,++pos))
    {
       if((even=++even % 2) == 0)
       t[pos]=distance;
    }
    return (t);
}


i would like to reverse the process now, letting x becoming l again!
thanks in advance!
The first thing I saw right away is if((even=++even % 2) == 0

That will result in undefined behavior. Perhaps try creating a copy variable of even and see if that helps.
Last edited on
@colorofjustice34

That will result in undefined behavior


No it has defined behaviour. This question was already discussed in the C++ Committee.
u have to defined behavior of the result.
Hope then will it work
@CLearner88



Maybe I have not understood correctly but can you simply exchange values of source and destination (not distance:)) in a call of the function?

Or is the algorithm the following that every 'x' following 'l' has to be changed again to 'l'?
Last edited on
@vlad

no it will take into account that lets say i have a string of ll, i will replace the 2nd occurrence of l with an x. It will take into account that every character that has a second occurrence in a row, it will replace that 2nd occurrence with an x.

lets say ll becomes lx, ww becomes wx. It is dependable on the first character.

ok, sure thing! thanks so much for your help!
Without testing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string RestoreEverySecondChar( const std::string &s, const char *src ) const
{
	//	src shall be "lx";
	std::string t( s );
	for ( std::string::size_type is_first = 0, pos = t.find_first_of( src, 0 ); 
	      pos != std::string::npos;
	      pos = t.find_first_of( src, ++pos ) )
	{
		std::string::size_type is_second = t[pos] == src[0]; 
		if ( is_first && !is_second  ) t[pos] = src[0];
		is_first = is_second;
	}

	return ( t );
}
Last edited on
> The first thing I saw right away is if((even=++even % 2) == 0
> That will result in undefined behavior.

Depends on a compiler switch. If C++11 mode is not enabled, it is undefined behaviour. Otherwise, it is well defined. In both, even = even++ % 2 results in undefined behaviour.

Just completely avoid this kind of stuff in the code that you write and/or use. Any code that requires interpretation by a language lawyer, when a simple transparent alternative is available, is badly written code.

you could do something like this to check the position
1
2
3
4
5
6
7
8
9
10
11
12
    std::string str = "hello world";
    unsigned int i = 0, count = 0;
    do {
        if(str[i] == 'l')
            count++;
        if(count < 2)
            i++;
    } while(count < 2);
    str[i] = 'x';
    std::cout << str << std::endl;
    str[i] = 'l';
    std::cout << str << std::endl;



//sorry didn't see that you meant only if its ll or ww then replace second char I thought you meant in general if you search for a character replace the second occurrence in the string
Last edited on
> It will take into account that every character that has a second occurrence in a row,
> it will replace that 2nd occurrence with an x.
> i would like to reverse the process now.

The function
std::string replace_second_consecutive_char_with_x( std::string ) ;
is not injective, and therefore it is not invertible.

For instance, these two distinct members of its domain
"Xerox xylophone boombox version xx meets Exxon minx 3" and
"Xeroo ylophone boxmboo version x mexts Eexon minn 3"

would map to the same member of its co-domain.
"Xerox xylophone boxmbox version xx mexts Exxon minx 3"

Just save a copy of the old string, and restore from the saved copy at a later time.
thanks guys for your help!
Really appreciate it!
Topic archived. No new replies allowed.