简体   繁体   English

在 function 中使用 strcpy() 交换字符串

[英]Swap strings using strcpy() in a function

i know this is a very simple question but i'm new and a bit down here.. i'm learning more about how to use functions, and i can get this to work if it was simply in main, or i can use another way, but i'm trying to solve it using strcpy(), and i dont want to change the main, everythijg should be in the function, any help is very much appreciated.我知道这是一个非常简单的问题,但我是新手,有点落后.. 我正在学习更多关于如何使用函数的知识,如果它只是在 main 中,我可以让它工作,或者我可以使用另一个方式,但我正在尝试使用 strcpy() 解决它,并且我不想更改主要内容,everythijg 应该在 function 中,非常感谢任何帮助。

char swap_char(char *s1, char *s2) {
    printf("before swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);

    char temp[50];
    
    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);
    
    printf("After swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);
}


main() {
    swap_char("please","work");
}
  1. "please" has 7 characters (including the terminator), but "work" only has 5 characters. "please"有 7 个字符(包括终止符),但"work"只有 5 个字符。 There is enough space for the first string to contain "work" , but the second string does NOT have enough space to contain "please" in it.第一个字符串有足够的空间来包含"work" ,但第二个字符串没有足够的空间来包含"please"

  2. Your code has literal strings "please" and "work" , which means they are very likely in READ ONLY memory, and cannot be changed or overwritten.您的代码有文字字符串"please""work" ,这意味着它们很可能在READ ONLY memory 中,并且不能更改或覆盖。

Fixing these two problems I get:解决这两个问题我得到:

char swap_char(char *s1, char *s2) {
    printf("before swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);

    char temp[50];
    
    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);
    
    printf("After swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);
}


int main() {
    char alpha[20] = "please";  // Declare space 20: MORE than enough space for either string
    char beta[20] = "work";     // Also, use a local array, which is NOT read-only, and can be changed.
    swap_char(alpha,beta);
    return 0;
}

Output Output

Success #stdin #stdout 0s 5524KB
before swapping : swap_char("please", "work")
After swapping : swap_char("work", "please")

The first step is reading the docs to see how it can be used and see if they have any helpful examples.第一步是阅读文档以了解如何使用它并查看它们是否有任何有用的示例。

The first issue here is we don't know if we will have enough space to complete this task.这里的第一个问题是我们不知道我们是否有足够的空间来完成这项任务。 Instead we can malloc space dynamically to make sure we don't run into issues.相反,我们可以动态地malloc空间来确保我们不会遇到问题。

// We don't need to return a char
void swap_char(char *s1, char *s2) {
    printf("before swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);

    // Allocate on the heap so we know we will never run out of space on a long input
    char temp = malloc(strlen(s1) + 1);
    
    strcpy(temp, s1);
    strcpy(s1, s2);
    strcpy(s2, temp);

    // Free temporary buffer
    free(temp);
    
    printf("After swapping : swap_char(\"%s\", \"%s\")\n", s1, s2);
}

However, there is a bigger problem here.然而,这里有一个更大的问题。 We don't know if both pointers have enough memory allocated.我们不知道两个指针是否分配了足够的 memory。 This is why this approach is not all that practical.这就是为什么这种方法不是那么实用的原因。 Also by passing the string pointers directly instead of using a pointer to a buffer on the stack or heap risks attempting to mutate read-only memory.此外,通过直接传递字符串指针而不是使用指向堆栈或堆上缓冲区的指针,可能会尝试改变只读 memory。 String constants are loaded along with the assembly instructions in each function into read-only memory on most modern systems.在大多数现代系统上,字符串常量与每个 function 中的汇编指令一起加载到只读 memory 中。 This is good since it prevents a malicious actor or undefined behavior from modifying the function assembly.这很好,因为它可以防止恶意行为者或未定义的行为修改 function 程序集。

char *a = "please";
char *b = "work";

// Create new buffers with the required space to use instead
unsigned int buffer_len = imax(strlen(a), strlen(b)) + 1;
char *a_buffer = malloc(buffer_len);
char *b_buffer = malloc(buffer_len);

// Copy the input strings into our new buffers
strcpy(a_buffer, a);
strcpy(b_buffer, b);

// Finally swap the buffers
swap_char(a_buffer, b_buffer);

As you can see, it isn't very practical but it is possible.如您所见,这不是很实用,但有可能。 A more practical approach is to just swap the pointers held by variables.一种更实用的方法是只交换变量持有的指针。

void swap_strings(char **s1, char **s2) {
    char *temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}


char *a = "please";
char *b = "work";

printf("Before swapping : swap_char(\"%s\", \"%s\")\n", a, b);
swap_strings(&a, &b);
printf("After swapping : swap_char(\"%s\", \"%s\")\n", a, b);

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

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