1

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. Kindly refer my Input and output given below to get clear idea.

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]
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Gokul UK
  • 49
  • 8
  • `fflush(stdin);` is defined as *Undefined Behavior* by the C standard, See, e.g. [C11 Standard - 7.21.5.2 The fflush function(p2)](http://port70.net/~nsz/c/c11/n1570.html#7.21.5.2p2) There is one non-standard conforming compiler that does allow it. – David C. Rankin Oct 19 '19 at 05:26

2 Answers2

1
  • First of all, don't use gets() as it is unsafe to use and also deprecated
  • p1 and p2 are dynamically allocated in the memory and they have their own size of n1 and n2 respectively. 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
  • 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
  • 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

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:

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
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • `p2 = malloc(n2 * sizeof(char));` (*too few by one*) Where does the nul-terminator go? (the same is probably true for `p1`, but you don't show the size that was input. The same will be true for `output` (e.g. `n1 + n2 + 1` chars required). The confusion is likely due to the mixing of `"size"` for `"length"` in the prompt. `sizeof(char)` is always `1` and should be omitted. (good job freeing what you have allocated) Always (always) ***validate the return*** of every input function use (and especially for `scanf`) – David C. Rankin Oct 19 '19 at 05:19
  • Still there is some issue with code @Rakibul. Added realloc function to add more memory but still final output is not perfect. Find the mentioned output in the code box. – Gokul UK Oct 19 '19 at 08:51
  • @DavidC.Rankin Thanks for the correction, I was in a hurry during writing this answer, I just missed the `null terminating character`. Now, I have edited the answer – UkFLSUI Oct 19 '19 at 22:09
  • @RakibulIslam that makes it better. Nice answer. – David C. Rankin Oct 19 '19 at 23:21
  • @RakibulIslam Sorry, I tried with online compiler, now its working but Im using codeblocks to learn C. In my compiler the output is not correct. In my complier output is like "Final copy is : gomicrosoft=". Another point is its giving different output for every time after printing "gomicrosoft". Eg: gomicrosoftM / gomicrosoft= / gomicrosoftW. Is there any problem with my compiler? If it is what should I do for correct it. – Gokul UK Oct 20 '19 at 08:26
  • @GokulUK Oh!!! Corrected it, check it out. My previous answer had undefined behaviour. Use `malloc` to new `output` pointer having size equal to `n1 + n2 + 1` – UkFLSUI Oct 20 '19 at 09:10
  • @RakibulIslam Executed successfully. – Gokul UK Oct 20 '19 at 09:41
0

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.

Gokul UK
  • 49
  • 8