1

This is actually just a small part of the original assignment I have in algorithms and data structure. At some point I'm supposed to write a function that swaps two elements in an array of strings.

The prototype we have to use is:

void swap(char *a, char *b);

To me it makes much more sense passing double pointers to this function (if swapping strings). In fact, I don't know how to make their prototype work without using double pointers. Am I missing something or is this wrong?

I've done my research on this a while ago and there are many answered questions here, I'm just wondering whether the person who was designing this assignment made a mistake.

13
  • 3
    You're right to query this. The assignment looks idiotic. Commented May 6, 2018 at 21:37
  • 2
    You can only use that swap function if a and b point to the same amount of memory (or an excessive amount of memory). E.g. char s1[100] = "hello", s2[100] = "world"; swap(s1, s2) Commented May 6, 2018 at 21:38
  • 3
    Have you been given sample code for the call site? Commented May 6, 2018 at 21:38
  • 1
    @user3386109 ah, you're right. Good point. Seems we have to make the assumption that enough memory is allocated at each pointer to contain either C-string Commented May 6, 2018 at 21:41
  • 2
    The accepted answer conflicts with the problem statement "write a function that swaps two elements in an array of strings" ... swapping elements is not the same as swapping strings. Commented May 6, 2018 at 22:47

1 Answer 1

3

You can swap two strings by creating a temporary char array of length equal to either one of the two strings, and by using strcpy().

Of course, you need to make sure that swapping does not overflow one of the two strings/arrays. Here is a sample example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void swap(char *a, char *b)
{
    char *tmp = malloc(strlen(a) + 1);

    strcpy(tmp, a);
    strcpy(a, b);
    strcpy(b, tmp);

    free(tmp);
}

int main(void)
{
    char str_a[50] = "Hello world";
    char str_b[50] = "What's up";

    swap(str_a, str_b);

    puts(str_a);
    puts(str_b);

    return 0;
}

Another approach is to use a loop with a single char variable, thus avoiding dynamic memory allocation:

void swap(char *a, char *b)
{
    char tmp;

    while (*a && *b)
    {
        tmp = *a;
        *a++ = *b;
        *b++ = tmp;
    }

    if (!*a)
    {
        size_t n = 0;
        while (b[n])
        {
            *a++ = b[n++];
        }
    }
    else if (!*b)
    {
        size_t n = 0;
        while (a[n])
        {
            *b++ = a[n++];
        }
    }
    *a = '\0';
    *b = '\0';
}
Sign up to request clarification or add additional context in comments.

6 Comments

@melpomene Because I use visual studio :)
That makes no sense. Do you mean you have it configured to use C++?
You only need one char of scratch space. You don't really need to malloc a whole string.
@melpomene Yes. The C compiler inside visual studio is very old, and disgusting.
@melpomene Do you mean, I can simply create a char variable, and use it in a loop to swap one character at a time?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.