Swapping array contents of different size

Hi all... I'm stumped on this problem and my array contents are not swapping correctly.

The output looks like this:
sssssss
aaa
aaassss
sss

I need the results to look like this (the first array is swapped with the second exactly as it was written):
sssssss
aaa
aaa
sssssss

I need to swap array "aaa" to the other array "ssssss". As you can see the array sizes are different.

Here is the 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
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

void switchthese (char[], char[]);
int main()
{
 char first[] = "sssssss";
 char second[] = "aaa";

cout<<first<<endl;
cout<<second<<endl;

switchthese(first,second);

cout<<first<<endl;
cout<<second<<endl;
}

void switchthese(char first[], char second[])
{
 int i = 0;
 while(first[i]!='\0' && second[i]!='\0')
  {
    char temp=first[i];
    first[i]=second[i];
    second[i]=temp;
    i++;
  }
}


If you need some clarification on something, please ask. Thank you all! I truly have tried to look around the forums for DAYS, but I cannot use pointers, or strlen and strcpy to complete this problem.
You cannot "swap" arrays of different sizes. Obviously the smaller array cannot hold as many elements as the larger array.
okay, the thing is I have solved the problem using a different method, but I cannot use this code in this case (I cannot use another function within a function and I did this to find the length of both arrays, can only use one loop in the swap function, and I can only use a null terminator or some other sentinel to stop the loop).

I was wondering if someone knows a way to write the working code below with these restrictions...The first post was my attempt at rewriting the code below. Thanks!

Results:
trembling
basic
basic
trembling

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
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

int findlength (const char[]);
void switchthese (char[], char[]);

int main()
{
 char first[] = "trembling";
 char second[] = "basic";

cout<<first<<endl;
cout<<second<<endl;

switchthese(first,second);

cout<<first<<endl;
cout<<second<<endl;
}

int findlength(const char thelength[])
{
  int n = 0;
  int i = 0;
  while(true)
  {
    if (thelength[i] == 0) break;
    i++;
    n++;
  }
  return n;
}

void switchthese(char first[],char second[])
{
  int length=findlength(first);
  if (findlength(first)>findlength(second))
    length=findlength(first);
  
  if (findlength(first)<findlength(second))
    length=findlength(second);
  
  int i = 0;
  while(true)
  {
    char temp=first[i];
    first[i]=second[i];
    second[i]=temp;
    i++;
    if(first[i]=='\0' && second[i]=='\0')break;
  }
  
}
Last edited on
okay, the thing is I have solved the problem using a different method,

No, you haven't. What you've done is invoke undefined behavior using a different method. Again, the array that is smaller than the other cannot hold the same number of elements the larger array holds.

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
#include <iostream>

void swap(char& a, char& b)
{
    char c = a;
    a = b;
    b = c;
}

// assumes both a and b point to enough memory to hold the 
// longest of the strings.
void swap_strings(char* a, char* b)
{
    unsigned i = 0, j = 0;
    unsigned eos_count = 0; // end-of-string count.

    while (eos_count < 2)
    {
        swap(a[i], b[j]);

        if (!a[i])
            ++eos_count;

        if (!b[j])
            ++eos_count;

        ++i, ++j;
    }
}

int main()
{
    char one[32] = "one";
    char three[32] = "three";

    std::cout << "one: \"" << one << "\"\n";
    std::cout << "three: \"" << three << "\"\n";

    swap_strings(one, three);

    std::cout << "one: \"" << one << "\"\n";
    std::cout << "three: \"" << three << "\"\n";
}


http://ideone.com/9ttJig
Is there a way to write that function without ampersands and pointers? The code looks great and it works, but I cannot use pointers and ampersands for this problem.

Also... I compiled the code from my second post without the length statements (they were remnants of a code that is no longer used):

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
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
void switchthese (char[], char[]);

int main()
{
 char first[] = "superb";
 char second[] = "excellent";

cout<<first<<endl;
cout<<second<<endl;

switchthese(first,second);

cout<<first<<endl;
cout<<second<<endl;
}


void switchthese(char first[],char second[])
{
  
  int i = 0;
  while(true)
  {
    char temp=first[i];
    first[i]=second[i];
    second[i]=temp;
    i++;
    if(first[i]=='\0' && second[i]=='\0')break;
  }
  
}


can anyone confirm if the code above abides by the following:
1) no other loops were used other than the while loop in the void function (if-break statements are not loops as far as I know).
2) I used a null terminator to end the loop (in this case '\0' which is the last character of all arrays)

also, I find it odd that the code compiles on compileonline.com, but it gives me an error in Visual Studio 2010...
Last edited on
Is there a way to write that function without ampersands and pointers? The code looks great and it works, but I cannot use pointers and ampersands for this problem.


There is no way to write a "swap" function without using pointers or references, no, but if you want to avoid the use of an ampersand or asterisk, that is relatively easy.

void switchthese(char* first, char* second)

is entirely equivalent to:

void switchthese(char first[], char second[])

first and second are pointers regardless of the notation used.

Your newest code still has the same problem all of your previous code has had. One of the arrays is not large enough to hold all of the elements in the other.

My previous code slightly modified:

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
#include <iostream>

// assumes both a and b point to enough memory to hold the 
// longest of the strings.
void swap_strings(char a[], char b[])
{
    unsigned i = 0, j = 0;
    unsigned eos_count = 0; // end-of-string count.

    while (eos_count < 2)
    {
        char c = a[i] ;
        a[i] = b[j] ;
        b[j] = c ;

        if (!a[i])
            ++eos_count;

        if (!b[j])
            ++eos_count;

        ++i, ++j;
    }
}

int main()
{
    char one[32] = "one";       // Notice the size of the arrays
    char three[32] = "three";

    std::cout << "one: \"" << one << "\"\n";
    std::cout << "three: \"" << three << "\"\n";

    swap_strings(one, three);

    std::cout << "one: \"" << one << "\"\n";
    std::cout << "three: \"" << three << "\"\n";
}


http://ideone.com/TFh0cR

Last edited on
This is where tuple unpacking can come in handy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <tuple>
using std::cin;
using std::cout;
using std::endl;


int main()
{
    const char *first = "sssssss";
    const char *second = "aaa";

    cout << first << endl;
    cout << second << endl;

    auto tup = std::make_tuple(second, first);
    std::tie(first, second) = tup;

    cout << first << endl;
    cout << second << endl;
    
    return 0;
}


http://coliru.stacked-crooked.com/a/9cba2917fba62ca9
This is where tuple unpacking can come in handy

The goal was to swap the contents of two arrays, not swap the content of two pointers.
> This is where tuple unpacking can come in handy

To swap two objects, use std::swap().
std::make_tuple() followed by std::tie() is convoluted (and inefficient).

In any case, swapping two pointers is not the same as swapping two arrays.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>

int main()
{
    const char* first = "sssssss";
    const char* second = "aaa";
    std::cout << first << ' ' << second << '\n' ;

    std::swap(first,second) ;
    std::cout << first << ' ' << second << '\n' ;
}
Topic archived. No new replies allowed.