work with strings | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

work with strings

From two eight-letter words to form a sequence of letters in which the letters of the first and second words should alternate

25th Mar 2020, 5:11 AM
Дима Надеин
Дима Надеин - avatar
7 Answers
0
If you are working with C++ strings, you should include <string> instead of <string.h>, because <string.h> contains the old C methods that operate on raw pointers. Also, you don't need an array of strings to hold the merged one, a single one is sufficient. https://code.sololearn.com/cPtZJTONvsfm/?ref=app
25th Mar 2020, 1:42 PM
Shadow
Shadow - avatar
+ 1
Because the loop index advances by 2 it skips letters. Also, slovo1[i+1] causes it to skip slovo1[0] and at the end to read slovo1[8], which is past the end of the string. Is that what you intended? From the problem description I think the output should be "rararararararara" instead of "rararar".
25th Mar 2020, 3:37 PM
Brian
Brian - avatar
0
Hey Дима Надеин, please specify what exactly your question is. Do you need help with creating such a program? In that case, please provide a code with what you have attempted to solve it by yourself, so we can point out any errors.
25th Mar 2020, 7:30 AM
Shadow
Shadow - avatar
0
Hello, I can’t write the program itself. I don’t know how to work with strings
25th Mar 2020, 12:15 PM
Дима Надеин
Дима Надеин - avatar
0
Since strings are char arrays, you can easily iterate them with a for-loop. Just set up a new string that can hold the necessary amount of characters, and then fill it by iterating over the strings and copying the characters alternately.
25th Mar 2020, 1:14 PM
Shadow
Shadow - avatar
0
I'm trying to do so #include <iostream> #include<string.h> using namespace std; int main() { string slovo1={rrrrrrrr}; string slovo2={aaaaaaaa}; string slovo[8]; int i; for(int i=0;i<8;i=i+2) slovo=slovo+slovo1[i]+slovo2[i+1]; cout <<endl<<slovo; return 0; }
25th Mar 2020, 1:22 PM
Дима Надеин
Дима Надеин - avatar
0
shadow thanks for explaining, you are my hero
25th Mar 2020, 1:48 PM
Дима Надеин
Дима Надеин - avatar