如何在C中交换字符串?

如何在C中交换字符串?,c,C,我正在尝试用C语言编写一个函数,它将交换两个字符串变量,但是出现了一些错误,程序崩溃了 请查看我的代码并告诉我哪里出错: #include <string.h> void strswap(char name1[], char name2[]) // to swap two strings { int lengthname1, lengthname2; lengthname1 = strlen(name1); lengthname2 = strlen(n

我正在尝试用C语言编写一个函数,它将交换两个字符串变量,但是出现了一些错误,程序崩溃了

请查看我的代码并告诉我哪里出错:

#include <string.h>

void strswap(char name1[], char name2[])    // to swap two strings
{
    int lengthname1, lengthname2;
    lengthname1 = strlen(name1);
    lengthname2 = strlen(name2);
    char temporaryname1[100];
    char temporaryname2[100];
    int x;
    int y;
    // till just the declaration
    for (int x = 0; x < lengthname1; lengthname1++) {
        temporaryname1[x] = name1[x];
        name1[x] = ' ';
    }
    // copying the value of name1 in temporaryname1
    for (int y = 0; y < lengthname2; lengthname2++) {
        temporaryname2[x] = name2[x];
        name2[x] = ' ';
    }
    // copying the value of name2 in temporaryname2
    for (int x = 0; x < lengthname1; lengthname1++) {
        name1[x] = temporaryname2[x];
    }
    for (int y = 0; y < lengthname2; lengthname2++) {
        name2[x] = temporaryname1[x];
    }
}


#include <stdio.h>
int main()
{
    char name[] = "hello";
    char name2[] = "hi";
    printf("before swapping: %s %s\n", name, name2);
    strswap(name, name2);
    printf("after swapping: %s %s\n", name, name2);
}
#包括
void strswap(char name1[],char name2[])//交换两个字符串
{
int lengthname1,lengthname2;
长度名称1=strlen(名称1);
长度名称2=strlen(名称2);
字符临时名称1[100];
字符临时名称2[100];
int x;
int-y;
//直到宣布
对于(int x=0;x
编辑:-我已经更正了程序,它工作正常。很快,我的头文件将与其他一些模块一起使用。感谢大家的帮助,尤其是@Micheal

void strswap(char ** name1, char ** name2)
{
    char * name1_1 = malloc(strlen(*name2) + 1);
    char * name2_1 = malloc(strlen(*name1) + 1);

    strncpy(name1_1, *name2, strlen(*name2) + 1);
    strncpy(name2_1, *name1, strlen(*name1) + 1);

    *name1 = name1_1;
    *name2 = name2_1;
}

int main()
{
    char * name="hello";
    char * name2="hi";
    printf("before swapping %s %s\n",name,name2);
    strswap(&name, &name2);
    printf("after swapping %s %s\n",name,name2);

    return 0;
}
实际上,下面是交换两个字符串最安全的方法。在您的例子中,静态地使用大小为100的字符串,这并不好,而且在所有情况下都可以工作。此外,您尝试使用“”而不是“\0”来标记字符串的结尾。字符串API使用“\0”表示字符串已结束。

存在许多问题:

第一期
x
变量未初始化:

    int x; int y;  // first declaration of x

    // till just the declaration
    for(int x=0;x<lengthname1;lengthname1++)
    {//  ^ second declaration of x , local to the loop
        temporaryname1[x]=name1[x];
        name1[x]=' ';
    }

  // if you use x here it's the first x that has never been initialized
第三期 在
main
中声明:

char name[] = "hello";
char name2[] = "hi";
这实际上与

char name[6] = "hello";  // 5 chars for "hello" + 1 char for the terminating NUL
char name2[3] = "hi";    // 2 chars for "hi" + 1 char for the terminating NUL
现在,即使您的
strswap
是正确的,您也试图将
name
数组(“hello”)中的6个字节填充到3个字节的数组
name2
,但是
name2
数组中没有足够的空间。这是未定义的行为

最后但并非最不重要的是: 这根本没用:

name1[x] = ' ';
最后
您应该问问自己,为什么在
strswap()
中需要两个临时字符串(
temporaryname1
temporaryname2
)-一个就足够了。

OT中的一些示例:
strswap
不是一个好名字(所有以
str
开头的名字都被保留)for循环将永远运行,因为x或y都不会递增。如果两个字符串的长度不同,您将如何交换它们?对于一般情况,使用像100这样的幻数似乎不是一个好主意,
malloc
free
会有帮助。@Mangu谢谢您,但是我的代码中出现了什么错误。你能告诉我吗?@d3l,现在答案好了吗?@ManguSinghRajpurohit现在你正在泄漏内存,你不需要
char**
在这里,
char*
就足够了。顺便说一句,strncpy(strncpy)
很糟糕,你可能会得到一个非NUL结尾的字符串。@ManguSinghRajpurohit现在更糟了。您没有针对空指针延迟进行保护,没有缓存
strlen
函数的结果,也没有检查
malloc
的返回值…@Micheal谢谢,因此如果我使用“name[100]和name2[100]”,那么它将是correct@SamratLuitel对但是您也需要更正strswap函数。@Micheal我已更正了所有内容,但它仍然不起作用。我可以在什么地方与您联系吗。还有一件事,如果不使用temporaryname1和temporaryname2,我如何交换两个字符串。如果我不使用name1[x]='',则一些数据将被重新混合。假设一个是hello,另一个是hi,那么交换时一个是hillo,另一个是hionly@SamratLuitel仔细查看您的代码。交换很容易:1。将字符串1复制到临时字符串2。将string2复制到string1、3。将临时字符串复制到string2@Micheal无论如何,我已经这样做了,但我忘记将“\0”字符复制到字符串中,因此它无法工作。
char name[] = "hello";
char name2[] = "hi";
char name[6] = "hello";  // 5 chars for "hello" + 1 char for the terminating NUL
char name2[3] = "hi";    // 2 chars for "hi" + 1 char for the terminating NUL
name1[x] = ' ';