简体   繁体   中英

How to swap 2 strings passed as pointers in a function?

I have to swap 2 char strings that I passed as pointers into a function: bool swap_strings (char* string1, char* string2) if both strings have not same length return false, else if swapping worked, return true;

how can I realise this? why is this not correct :

#include <iostream>

using namespace std;


bool swap_strings(char * string1, char * string2) { 
    char * s;
    char * s2;
    char * tmp = nullptr;
        for (s = string1; *s != '\0'; ++s);
        for (s2 = string2; *s2 != '\0'; ++s2);
        if ((s - s2)  != 0) {
            return false;
        }
        else {      
            tmp = *&string1;
            string1 = *&string2;
            string2 = *&tmp;
            cout << tmp << string1 << string2<< endl;
        }

        return true;
    }



int main()
{

    bool da = false;
    char s1[] = "hallo1";
    char s2[] = "ababab";

    da = swap_strings(s1, s2); 

    cout << da << s1 << s2 <<endl;
}   

Unless you want to duplicate code that is already written, you'll need to use strlen to get the length of the strings:

unsigned int s1len = strlen(string1);
unsigned int s2len = strlen(string2);
if (s1len != s2len)
    return false;

If you cannot use the standard library functions (silly requirement):

unsigned int s1len = 0;
for (char* s = string1; *s != '\0'; s++, s1len++);

Will do the same thing (you have to do it for both strings).

After that, the swap is simple:

for (unsigned int i = 0; i < s1len; ++i)
{
    std::swap(string1[i], string2[i]);
}

or

for (unsigned int i = 0; i < s1len; ++i)
{
    // these 3 lines are identical to what is done in std::swap
    char t = string1[i];
    string1[i] = string2[i];
    string2[i] = t;
}

You can also (defeating the purpose of the assignment, no doubt), simply change the pointer values:

bool swap_strings(char*& string1, char*& string2)
{
    char* t = string1;
    string1 = string2; // string1 will now point to string2
    string2 = t; // string2 will now point to what use to be string1
    return true;
}

I say it defeats the purpose of the assignment as you are not doing any copying, and the length requirement is irrelevant when you do it this way. It also will not work (eg will not compile) when you are passing in arrays (eg if you declared your inputs as char a[] = "abcd"; - which you have done in your main ).

You can simply loop through the passed strings, and do a character-by-character swap.

eg

#include <assert.h>
#include <iostream>

bool swap_strings(char * const s1, char * const s2) {
    assert(s1 != nullptr);
    assert(s2 != nullptr);
    if (strlen(s1) != strlen(s2))
        return false;

    char * pch1 = s1;
    char * pch2 = s2;
    char temp;
    while (*pch1 != '\0') {
        temp = *pch1;
        *pch1 = *pch2;
        *pch2 = temp;

        ++pch1;
        ++pch2;
    }
    return true;
}

int main() {
    using namespace std;

    char s1[] = "hallo1";
    char s2[] = "world2";

    if (swap_strings(s1, s2)) {
        cout << s1 << endl;
        cout << s2 << endl;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM