I have two strings and want to intertwain the elements into an new array. Does anyone know of a way to do this?
I have two strings and want to intertwain the elements into an new array. Does anyone know of a way to do this?
How exactly do you want to combine them?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
for example:
string 1 = code
string 2 = area
I want the result string to be cAoRdEeA
but i am having a difficult time coming up with the loop.
Any help?
You would want to loop from 0 to the size of the smaller string, right? Start with that.
yeah, but I don't know how to include the other characters from the other string in betwwen. I am completely stuck at this point. Can anyone offer a suggestion?
// pseudo code
--Code:for each element in the shorter (astring, bstring) result += one letter from astring + one letter from bstring; if lengths aren't equal add remaining in the longer (astring, bstring) to result
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
the loop would skip an array location, right? (0, 2 4, etc). That's enough clues.
matsp, I am drawing a blank as I have never coded this before:
what would be the update on the (for?) loop for your suggestion?
How can I skip an aray location????
this is what I have:
str3 is string 1...Code:for (b = 0; b < str3.length(); b++) { result[b] = str3[b]; result[b + 2] = str3[b+1]; }
Since you are using C++ strings, you don't need to worry about indexing the destination.
But you do need to grab a character from each of the two strings. Your code is only taking from the variable called "str3".
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
str3 is not a variable, it is a string.
How could I do this without indexes. I am in need of urgent help.
What about this:
if I have "are", I will getCode:for (b = 1; b < str3.length(); b++) { result[0] = str3[0]; result[b + 2] = str3[b]; } result [totlength] = 0; cout << result << endl;
a==re=
for the cout.
what about this:
Code:int a = 0; int l = 1;// initialize the Array Indices int b = 0; int c = 0; result[0] = str3[0]; result[1] = str2[0]; for (b = 1; b < str3.length(); b++) { result[a + 2] = str3[b]; a++; } for (c = 1; c < str2.length(); c++) { result[l + 2] = str2[c]; l++; } result [totlength] = 0; cout << result << endl;
Ok, i got it merging them for equal lengths. Now, my question is, how do I tell the program that if the second string is shorter , just to print out the rest of string 1? using the structure I used above?
here is what i have:
Code:int a = 0; int l = 1;// initialize the Array Indices int b = 0; int c = 0; result[0] = str3[0]; result[1] = str2[0]; for (b = 1; b < str3.length(); b++) { result[a + 2] = str3[b]; a++; a++; } for (c = 1; c < str2.length(); c++) { result[l + 2] = str2[c]; l++; l++; } result [totlength] = 0; cout << result << endl; }
how can I address this? anyone?