简体   繁体   English

如何交换在函数中作为指针传递的2个字符串?

[英]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; 我必须交换2个作为指针传递给函数的char字符串:bool swap_strings(char * string1,char * string2)如果两个字符串的长度不相同则返回false,否则如果交换工作,则返回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: 除非您想要复制已编写的代码,否则您需要使用strlen来获取字符串的长度:

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 ). 当你传入数组时它也不会工作(例如不会编译)(例如,如果你将你的输入声明为char a[] = "abcd"; - 你已经在你的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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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