codescracker
c

C Program to Copy One String to Another



« Previous Program Next Program »

In this tutorial, we will learn about how to copy one string to another in C language, with and without using library function strcpy(). At last, we will also learn, how to copy string using pointer. Let's first start with copy string using library function.

Copy String in C using strcpy() Function

To copy string in C programming, you have to ask from user to enter the string to store it in first string variable say str1 and then copy it into the second string variable say str2 using the strcpy() function of string.h library. The function strcpy() takes two argument. The value of second argument gets copied to first argument.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[20], str2[20];
    printf("Enter the string: ");
    gets(str1);
    printf("\nString 1 = %s", str1);
    strcpy(str2, str1);
    printf("\nString 2 = %s", str2);
    getch();
    return 0;
}

As the above program was written in Code::Blocks IDE, therefore after successful build and run, here is the first screenshot of the sample run that you will also see on your output screen after running the program:

c program copy string

Now supply any string say codescracker and press ENTER key to see the similar output as given here. This is the second snapshot of the sample run:

copy string c

Here is another sample run, where user enters any string that contains space:

copy string c language

Program Explained

  1. Get string input from user using gets() function
  2. Before using gets() function, include string.h library function
  3. Now copy the string into another variable say str2 here using strcpy() function
  4. The strcpy() function takes two argument, first argument is the target variable where the string is going to be copy
  5. And the second argument is the source variable. The value of this variable is initialized to the target variable
  6. And finally print the value of both the variable

As you can see from previous program, I've used gets() function to get the string input. Because if we use scanf() function and user enters the string that contains spaces, then after space all the string's values gets skipped. For example, if the above program is replaced with following program:

#include<stdio.h>
#include<conio.h>
int main()
{
    char str1[20], str2[20];
    printf("Enter the string: ");
    scanf("%s", str1);
    printf("\nString 1 = %s", str1);
    strcpy(str2, str1);
    printf("\nString 2 = %s", str2);
    getch();
    return 0;
}

Now let's take a sample run of the above program. Here I've supplied the input as codes cracker. The string contains space, therefore after space, rest of the string value gets skipped and only codes is initialized to the variable str1 as shown in the following snapshot:

string copy c program

As you can see from the above snapshot, only codes is initialized to the variable str1 and the value of str1 gets copied to the str2 variable. Therefore, the above program is correct only when user doesn't supply any string with spaces.

Copy String in C without using strcpy() Function

This program does the same job as of previous one, but without using any library function strcpy():

#include<stdio.h>
#include<conio.h>
int main()
{
    char strOrig[100], strCopy[100], i;
    printf("Enter any string: ");
    gets(strOrig);
    for(i=0; strOrig[i]!='\0'; i++)
        strCopy[i] = strOrig[i];
    strCopy[i] = '\0'; // never forgot to set null terminated character at the end of string
    printf("\nString 1 = %s", strOrig);
    printf("\nString 2 = %s", strCopy);
    getch();
    return 0;
}

Here is the final snapshot of the first sample run:

c copy string without library

Here is the final snapshot of the second sample run:

copy string without library function c

Program Explained

Copy String in C using Pointer

Now let's create the same purpose program using pointer:

#include<stdio.h>
#include<conio.h>
void copystr(char *, char *);
int main()
{
    char sourceStr[50], targetStr[50];
    printf("Enter the string: ");
    gets(sourceStr);
    printf("\nString 1 = %s", sourceStr);
    copystr(targetStr, sourceStr);
    printf("\nString 2 = %s", targetStr);
    getch();
    return 0;
}
void copystr(char *targetString, char *sourceString)
{
    while(*sourceString)
    {
        *targetString = *sourceString;
        sourceString++;
        targetString++;
    }
    *targetString = '\0';
}

Here is the sample run:

c program copy string using pointer

Now enter any string say codescracker and press ENTER key to see the output as given in the second screenshot here:

copy string using pointer c program

Program Explained

  1. Here we've created a function named copystr() that has two pointer arguments
  2. The first argument is responsible to the target string and the second one is for source string
  3. After calling the function copystr(), the target string and source string gets passed as argument
  4. And we knows that source string holds the value that user enters at run-time. But target string has some garbage values by default
  5. Now we've created a while loop inside the function that runs before the source string comes at its null terminated character, that is '\0' (or runs until the last character of the string)
  6. Inside the loop we have copied each character (one by one) of the source string into the target string
  7. After copying or initializing, we have incremented both the pointer variable of target and source string
  8. After completing the while loop, all the character of source string gets copied to the target variable
  9. As we have used pointer here, therefore we only have to output the value of both the string
  10. Never forgot to place null terminated character ('\0') at end of the targetString pointer variable

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker