0

Help. I submitted a homework to a page and it gave 0/20

You are given two strings. You need to merge them intermittently as shown below. Input acdefghijkl xyza23 Output axcydzeaf2g3hijkl The example i submitted:

23rd May 2018, 4:13 PM
Regi
Regi - avatar
13 ответов
+ 1
You can accomplish this with just one loop instead. Use the iterator variable in the loop to keep track of the position between the two arrays, and that's how you'll access the stored value for each during each iteration of the loop. Then inside of the loop, have it assign the current value from the first array to your result array, and then the value from the second array to your result array. When it's done looping, it'll have generated the string you're seeking. Btw, the code you posted appears to be perfectly fine. So I'm not sure what your issue is. It's not how I would, or recommend, going about resolving it, but it should still work as expected.
23rd May 2018, 4:18 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Okay. This what you're trying to do? https://code.sololearn.com/cM3Z6jaS7w16/#cpp #include<iostream> #include <strings.h> using namespace std; int main() { int maxIterations = 0; char st1 [] = "abcdefgh"; char st2 [] = "tuvwxyz"; string res = ""; if(strlen(st1) > strlen(st2)) maxIterations = strlen(st1); else maxIterations = strlen(st2); for(int i = 0; i < maxIterations; ++i){ res.push_back(st1[i]); res.push_back(st2[i]); } cout << res << endl; return 0; } :::: OUTPUT ::::: atbucvdwexfygzh
23rd May 2018, 4:45 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Regi can I see the site?
23rd May 2018, 7:21 PM
Cain Eviatar
Cain Eviatar - avatar
+ 1
It is a site created by our teacher.He post them in piazza
23rd May 2018, 7:22 PM
Regi
Regi - avatar
+ 1
Regi okay, now for your code, I can make it and show you my way in C. if you'd like to .
23rd May 2018, 7:24 PM
Cain Eviatar
Cain Eviatar - avatar
0
#include <stdio.h> #include <strings.h> int main() { char st1 [] = "abcdefgh"; char st2 [] = "tuvwxyz"; char res[30]; int x,y; for(y = 0,x = 1; x < 30, y < strlen(st2); x+= 2, y++){ res[x] = st2[y]; } for(y = 0,x = 0; x < 30, y < strlen(st1); x+= 2, y++){ res[x] = st1[y]; } printf(" %s", res); return 0; }
23rd May 2018, 4:13 PM
Regi
Regi - avatar
0
I need to do it with strings
23rd May 2018, 4:19 PM
Regi
Regi - avatar
0
You could replace char [] with string. Strings support using [] to access single characters
23rd May 2018, 5:01 PM
Max
Max - avatar
0
Regi this is almost not C++. you shouldn't code like that in C++, it's more C style. C++ is object oriented, which means you have classes and objects. replace strings.h with string. to using string object from C++98. strlen shouldn't be used, you can use str.length(), and you can access the chars array, by using the bracket [].
23rd May 2018, 7:14 PM
Cain Eviatar
Cain Eviatar - avatar
0
The site that i subimt my execrsices didn't accept it.How should i write it?
23rd May 2018, 7:16 PM
Regi
Regi - avatar
0
Regi this is weird, are you sure the site support C++ and not C?
23rd May 2018, 7:19 PM
Cain Eviatar
Cain Eviatar - avatar
0
Yes because you can change the programming languages.It gave me 0/20 points
23rd May 2018, 7:21 PM
Regi
Regi - avatar
0
Will that help me because i need it in c++
23rd May 2018, 7:25 PM
Regi
Regi - avatar