string concatenation

I am making a function where I concatenate a string, and test to see if the string has the same characters and if they do have the same characters I have to delete one of them.

Is the += a good option for that?

function:
1
2
3
4
5
6
7
8
9
10
11
string ConcatWithoutDouble(string string1,string string2)
{   strcat(string1,string2);
    for(int i=0;i<strlen(string1);i++)
        {
            if (string1[i]==string1[i+1])
                {
                    string1[i]+=string1[i+1]
                }
        }

}
Last edited on
It doesn't make sense to me to do that on line 7, is their something I can do to delete one of the characters?
First: strcat() and strlen() are not for use on strings; they are C functions for C-style strings (character arrays). For strings, if you want to concatenate them use string1 + string2 and if you want the length use string1.length().

I think you could implement your code much more simply than you have here; just test if the strings are identical; if they are, return one of them, otherwise return the concatenation. There is no need to go character by character.
Thanks for the advice on adding strings and how to get length.
The thing is in my main the strings are not the same

Main:
1
2
3
4
5
6
7
8
    string a = "oompa";
    string b = "aaaloompa";

    cout << ConcatWithoutDouble(a, b) << endl;
    a = "abba";
    b = "azabba";

    cout << ConcatWithoutDouble(a, b) << endl;


When I encounter two letters that repeat itself then I have to remove one of them.

Function:
1
2
3
4
5
6
7
8
9
10
11
string ConcatWithoutDouble(string string1,string string2)
{   string string3=string1 + string2;
    for(int i=0;i<string3.length();i++)
        {
            if (string3[i]==string3[i+1])
                {
                    string3[i]+=string3[i+1];
                }
        }

}


I ran my code but my program ended up crashing. :(
Last edited on
would the erase operator solve my issue?
Last edited on
Oh, I don't think I had properly understood your goal.

I believe one possible problem is that when i is the position of the last character of the string, you check i+1 which is past the end of the string.

A simple algorithm to do what you are trying to could be to just step through string3, looking at each character, and adding it to a new string if it isn't identical to one you just added. This can save you from having to iterate the string as you remove characters from it.

If you do want to do that, I'd recommend using iterators to go through the string and use erase():
1
2
3
4
5
6
7
8
9
10
11
12
for(auto i = string3.begin(); i != string3.end(); /* manually increment */) {
    // i refers to a character in the string
    char characterWeAreLookingAt = *i;
    // You can use erase to remove the character i refers to
    // Make sure to note that erase returns what the iterator should be changed to!
    // Only increment if you don't want to erase!
    if(we_want_to_erase_i) {
        i = string3.erase(i);
    } else {
        ++i;
    }
}

You'll need some more work to identify whether you need to erase i or not.
Last edited on
on line 7
Would it be better if i checked the characterWeAreLookingAt against the last one?

if it is not the same the on line 10 i would increase by 1, then it can check all the way to the end of the string.
Is that what you want to do? Whatever condition you want to use to decide whether you want to remove the character or not needs to go there. If you want to compare against the previous character, though, you'll probably want to save that somewhere, probably when in the else block.
Zhuge thanks for the outline of what I was supposed to do, I used it as a base for my code.

There were some things I had to change though, I have not gone over some of the things such as auto and I had to change some other things around as well.

ok this is what I wrote, I have the gist of what I want to do but there are still bugs in my code. When I run my code it only prints out one word.

Function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string ConcatWithoutDouble(string string1,string string2)
{   string string3=string1 + string2;
    for(int i= 0; i < string3.length()-1; /* manually increment */)
        {
    // i refers to a character in the string
    char characterWeAreLookingAt = string3[i];
    char characterWeAreLookingAt1=string3[i+1];
    // You can use erase to remove the character i refers to
    // Make sure to note that erase returns what the iterator should be changed to!
    // Only increment if you don't want to erase!
    if(characterWeAreLookingAt==characterWeAreLookingAt1) {
        string3.erase(i--);
    } else {
        ++i;
    }
}

    return string3;
}
Topic archived. No new replies allowed.