codescracker


c

C Program to Copy String



« Previous Program Next Program »

In this tutorial, we will learn about how to copy string in C language with and without using library function. 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

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. And finally print the value of both the string. You will see the same output as the second string variable also holds the same value that the first string variable holds after copying.

C Programming Code to Copy String

Following C program asks from the user to enter any string to copy it into another variable of same type (char), then display the copied string on the screen as shown here:

// Write a program in C to copy string
// -----------codescracker.com---------------

#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 is written in Code::Blocks IDE, therefore after successfully 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

As you can also see that both the string holds the same values. Now let's take a look at some of the main steps used.

The main steps used in the program given above are:

  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

Here I've used gets() function to get the string input. Because if we use scanf() function and user enters the string that contains space, then after space all the string's values gets skipped. For example, if the above program is replaced with the below one. Here in place of gets(), I've used scanf() function to get the input from user:

// Write a program in C to copy string. This program is correct only
// when user supplies any string without spaces
// -----------codescracker.com---------------

#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.

Here is the snapshot of the sample run:

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 without Library Function

Here is another program that will copy one given string into another string without using any library function and normal function:

// Write a program in C to read a string and copy it into
// another string.(Do not use Library Function)
// -----------codescracker.com-----------

#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

Here are some of the main steps used in above program:

Copy String using Pointer in C

Now let's create the same program that is to copy the content of one string into another string but using pointer method this time. Here is the program:

// Write a program in C to copy the content of
// one string to another using a pointer method
// ----------codescracker.com------------

#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';
}

The above program is written under Code::Blocks IDE, therefore after successfully build and run you will get the output as shown in the sample run given here:

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

The main logic used in the program given here are:

  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 Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test