Interchanging letters of 2 strings. PLS HELP!

Hello, Ive been struggling with this question & I'd really appreciate it if someone helped me out. Thanks in advance!

Question:
Mix characters of two string such that if
string1: Girly
string2: Sun
string3 should be: GSiurnly

note that the interchanging happened only the bigger string was equal to the smaller one, then the rest of the bigger string was set at the end.

My code:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
int size1,size2,newsize,size1original,size2original,difference,index1=0,index1b=0,index2=1,index2b=0;
	
	char string1[50];
	char string2[50];
	char string3[100];
	
	
	cout<<"Input the first string: ";
	cin>>string1;
	
	cout<<"\nInput the second string: ";
	cin>>string2;
	
	size1original=strlen(string1);
	size2original=strlen(string2);
	
	size1=strlen(string1);
	size2=strlen(string2);

	while ( size1!=0 || size2!=0 )
	{
		
		string3[index1]=string1[index1b];
		index1=index1+2;
		index1b++;
		size1--;
		
		string3[index2]=string2[index2b];
		index2=index2+2;
		index2b++;
	        size2--;
	   
	}
	
	newsize=size2original*2;
	difference=size1original-size2original;
	
	if (size1original>size2original)
	{
		while (difference!=0 )
		{
		
		string3[newsize]=string1[size2original];
		newsize++;
		size2original++;
		difference--;	
			
			
		}	
	}
	newsize=size1original*2;
	difference=size2original-size1original;
	
		if (size2original>size1original)
	{
		while (difference!=0 )
		{
		
		string3[newsize]=string1[size1original];
		newsize++;
		size1original++;
		difference--;	
			
			
		}	
	}
	cout<<string3;
	return 0;
}
Is it not working? Please explain what problem you are having, then ask a question.
Well, the code works only when the two strings are equal in size. But when the two strings arent equal in size, infinite loop occurs.
Do you have any idea what's wrong?
Thank you in advance.
Do you think lines 43 and 59 should both be operating on string1?
First you need to find out the length of the shortest string. Then you merge the two strings for the length of the shortest. If one string is longer then you just add the rest of the longest string to the output.

I have written an example but used pointers so I am not sure if it would help you.
First you need to find out the length of the shortest string.

You don't actually need to find out the length of either string before traversing them. It's simpler to just depend on encountering the nul terminator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void intermix(const char* a, const char* b, char* result)
{
    unsigned a_idx = 0;
    unsigned b_idx = 0;
    unsigned r_idx = 0;

    while (a[a_idx] || b[b_idx])
    {
        if (a[a_idx])
            result[r_idx++] = a[a_idx++];

        if (b[b_idx])
            result[r_idx++] = b[b_idx++];
    }

    result[r_idx] = '\0';
}


I think it's a little cleaner without the array subscript notation:

1
2
3
4
5
6
7
8
9
10
11
12
void intermix(const char* a, const char* b, char* result)
{
    while (*a || *b)
    {
        if (*a)
            *result++ = *a++;
        if (*b)
            *result++ = *b++;
    }

    *result = '\0';
}


It's simpler to just depend on encountering the nul terminator.


You are right. My idea was too complicated, but I have little experience with pointers.
That was my idea.
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
void MergeStrings(char *s1, char *s2, char *output)
{
  char *pShorter, *pLonger, *pOut;
  
  if (strlen(s1) <= strlen(s2))
  {
    pShorter = s1;
    pLonger = s2;
  } 
  else
  {
    pShorter = s2;
    pLonger = s1;
  }
  pOut = output;
  while(*pShorter)
  {
    *pOut++ = *pShorter++;
    *pOut++ = *pLonger++;
  }
  while( *pLonger)
  {
    *pOut++ = *pLonger++;
  }
  *pOut++ = NULL;
}
Topic archived. No new replies allowed.