简体   繁体   English

在 Dynamic 中使用指针将一个字符串复制到另一个字符串中

[英]Copy a String into another String by using pointers in Dynamic

Trying to copy a string to another string.试图将一个字符串复制到另一个字符串。 As a basic learner I have tried maximum from my side to get the output but in the end of the program(point1) my logic is not working proper.作为一个基本的学习者,我已经尽力从我身边获得 output 但在程序结束时(point1)我的逻辑无法正常工作。 Kindly refer my Input and output given below to get clear idea.请参考下面给出的我的输入和 output 以获得清晰的想法。

This Program copy a string.这个程序复制一个字符串。

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

int main()
{
    int n1,n2,loc;
    char *p1, *p2;

    printf("Enter size of p1\n");
    scanf("%d", &n1);

    p1 = (char*) malloc (n1 * sizeof(char));

    printf("Enter the P1 String\n");
    fflush(stdin);
    gets(p1);

    printf("\nEnter the size of p2\n");
    scanf("%d", &n2);

    p2 = (char*) malloc (n2 * sizeof(char));

    printf("Enter the P2 String\n");
    fflush(stdin);
    gets(p2);

    printf("\nEnter the Location to copy\n");
    scanf("%d", &loc);

    for(int i=loc-1;i<=n1;i++) //point 1
    {
       *(p1+i) = *(p1+i)+n2;
    }

    for(int i=0;i<=n2;i++)
    {
       *(p2+i) = *(p1+i)+loc;
    }

    printf("\n Final copy is\n");
        printf("%d",p1);

    free(p1);
    free(p2);

    return 0;
}

Expected:预期的:

Input:
google
microsoft

output:
Goomicrosoftgle

Actual:实际的:

Input:
google
microsoft

output:
[Some garbage values including given string]
  • First of all, don't use gets() as it is unsafe to use and also deprecated首先,不要使用gets() ,因为它使用起来不安全,也被弃用了
  • p1 and p2 are dynamically allocated in the memory and they have their own size of n1 and n2 respectively. p1p2在 memory 中动态分配,它们分别有自己的大小n1n2 So, when you add more characters from p2 into p1 , the size of output needs to be increased, otherwise they wouldn't fit in that memory space因此,当您将更多字符从p2添加到p1时,需要增加 output 的大小,否则它们将不适合该 memory 空间
  • For string input using scanf the allocated memory should have to be one more than the actual length as one null terminating character is inserted at the end对于使用scanf的字符串输入,分配的 memory 应该比实际长度大一,因为在末尾插入了一个 null 终止字符
  • In your final print statement, you are using %d which is the format specifier for integer type, so you should use %s as you are printing out whole string在您的最终打印语句中,您使用的是%d ,它是 integer 类型的格式说明符,因此您应该在打印整个字符串时使用%s

So, this should work:所以,这应该工作:

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

int main()
{
    int n1, n2, loc;
    char *p1, *p2, *output;

    printf("Enter size of p1: ");
    scanf("%d", &n1);

    p1 = malloc((n1 + 1) * sizeof(char));

    printf("Enter the P1 String: ");
    scanf("%s", p1);

    printf("\nEnter the size of p2: ");
    scanf("%d", &n2);

    p2 = malloc((n2 + 1) * sizeof(char));

    printf("Enter the P2 String: ");
    scanf("%s", p2);

    printf("\nEnter the Location to copy: ");
    scanf("%d", &loc);

    output = malloc((n1 + n2 + 1) * sizeof(char));

    for (int i = 0; i < loc; i++)
        *(output + i) = *(p1 + i);

    for (int i = loc - 1; i <= n1; i++)
        *(output + i + n2) = *(p1 + i);

    for (int i = 0; i < n2; i++)
        *(output + i + loc) = *(p2 + i);

    printf("\nFinal copy is: ");
    printf("%s\n", output);

    free(p1);
    free(p2);
    free(output);

    return 0;
}

Here's the output:这是 output:

Enter size of p1: 6
Enter the P1 String: google

Enter the size of p2: 9
Enter the P2 String: microsoft

Enter the Location to copy: 3

Final copy is: goomicrosoftgle

Here is the correct logic of copy a string using dynamic method.这是使用动态方法复制字符串的正确逻辑。

output = malloc((n1 + n2 + 1) * sizeof(char)); //After allocating a memory to both sizes of string along with adding memory for NULL. //在为两种大小的字符串分配 memory 并为 NULL 添加 memory 之后。

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

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