@An_Integer, I'm trying to work out how many lines of your "solution" make any sense at all.
If a "friend" gave it to you then you've been had!
If you re-order the declarations of charArray and anotherArray ... you will (probably) get a different answer. What do you think the line if (charArray < anotherArray)
does? That would compare memory locations, not c-string lengths.
Also, charArray[(sizeof(charArray) - 255) + i]
is a pretty daft (and non-robust) way of writing charArray[i+1]
Try:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <cstring>
usingnamespace std;
int main()
{
char charArray[256]="12345";
char anotherArray[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << charArray << '\n';
if ( strlen(charArray) < strlen(anotherArray) )
{
int charArraySize = strlen(charArray);
for (int i = charArraySize; i < strlen(anotherArray); i++) charArray[i] = charArray[i-charArraySize];
charArray[strlen(anotherArray)] = '\0';
}
cout << charArray << '\n';
}
Thank you so much @lastchance! That really did the trick. The "thing" I wrote was apparently complete bogus. I should edit the reply and give a warning about it. Because this:
"If you re-order the declarations of charArray and anotherArray ... you will (probably) get a different answer. What do you think the line..."
Saved the whole program. I was having problems doing other things with the program, even though it worked fine with what I first wrote.