string copy...weird issue.

Jun 1, 2017 at 1:02pm
I have written a program using loops that combines two strings into one using a loop. it works fine but the output line is corrupt for some reason. code and screenshot attached.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include "Q1.h"
#include "Q3.h"
#include <string>
using namespace std;

void  merge(char first[], char second[], char result[])
{
	int length = strlen(first)+strlen(second);
	
	for (int i = 0 , j = length ; i < j ; i++ , j--)
	{
		result[i] = first[i];
		result[j] = second[j];
	}

}

Image link:
https://postimg.org/image/gxyp1yzh1/
Jun 1, 2017 at 1:14pm
The first time round the loop, the code looks like this:

1
2
	result[0] = first[0];
	result[length] = second[length];


result[length] is after where you expect the last character you write into result to go, so that's clearly wrong.
second[length] is off the end of second.
Jun 1, 2017 at 1:25pm
Fixed that issue by substracting strlen from j, thanks! but I'm still getting this corrupt output.
Now only one of these signs appear between the strings...
Jun 1, 2017 at 1:28pm
That would suggest that you're starting to write the second string into the wrong place. That you're off by one in your calculations.

Start debugging. This means start writing out the values of i and j as you calculate them, and identify where you're going wrong.

Last edited on Jun 1, 2017 at 1:29pm
Jun 1, 2017 at 1:33pm
Solved. thanks!
Jun 1, 2017 at 2:18pm
and life gets much simpler using std::string:
1
2
3
4
5
6
7
8
9
10
11
12
# include <iostream>
# include <string>

int main()
{
    std::string a = "hello";
    auto b = "world";

    auto c = a + " " + b;
    std::cout << c << "\n";
}
Jun 1, 2017 at 3:26pm
Easiest and best way is gunnerfunner's way, but if you have to use old-style C strings...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <string.h>

void  merge(char first[], char second[], char result[])
{
  char *cp = first;
  /*copy first to result */
  while(*cp)
  {
    *result = *cp;
    result++;
    cp++;
  }
  cp = second;
  /* add second to result */
  while(*cp)
  {
    *result = *cp;
    result++;
    cp++;
  }
  *result = '\0';
}

int main(void)
{
  char firstname[] = "Anna ";
  char lastname[] = "Lazareva";
  char fullname[32];

  merge(firstname, lastname, fullname);
  printf("Fullname = %s", fullname);
}


OUTPUT
Fullname = Anna Lazareva
Jun 1, 2017 at 3:36pm
j isnt right.

look:


int length = strlen(first)+strlen(second);

so length is the TOTAL length of both strings.


for (int i = 0 , j = length ; i < j ; i++ , j--)
{
result[i] = first[i];
result[j] = second[j]; ///for 1234 1234 you start writing at the 8th position!
}


come to think of it the logic is not quite right either with the -- and trying to bounce off total length etc.

try something along these lines:

int flen = strlen(first);
int s = 0;
for(i = 0; i < length; i++)
{
if(i < flen)
result[i] = first[i];
else
result[i] = second[s++];
}
result [i] = 0; //important to terminate your string...

Topic archived. No new replies allowed.