C program to copy one string to another string

Write a C program to copy one string to another string using loop. C program to copy one string to another without using inbuilt library function strcpy(). How to copy one string to another without using inbuilt string library function in C programming. Effective logic to copy strings in C programming. How to copy one string to another string using strcpy() function in C program.

Required knowledge

Basic C programming, C for loop, Array, Pointer, functions, Strings

Logic to copy one string to another string

Below is the step by step descriptive logic to copy one string to another string.

  1. Input string from user and store it to some variable say text1.
  2. Declare another variable to store copy of first string in text2.
  3. Run a loop from 0 to end of string. The loop structure should be like for(i=0; text1[i] != '\0'; i++).
  4. Inside the loop for each character in text1 copy to text2. Say text2[i] = text1[i].
  5. Finally after loop make sure the copied string ends with NULL character i.e. text2[i] = '\0';.

Program to copy string without using strcpy()



 
/**
 * C program to copy one string to another string without using strcpy()
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text1[MAX_SIZE];
    char text2[MAX_SIZE];
    int i;
    
    /* Input string from user */
    printf("Enter any string: ");
    gets(text1);
    
    /* Copy text1 to text2 character by character */
    for(i=0; text1[i]!='\0'; i++)
    {
        text2[i] = text1[i];
    }

    //Makes sure that the string is NULL terminated
    text2[i] = '\0';

    printf("First string = %s\n", text1);
    printf("Second string = %s\n", text2);
    printf("Total characters copied = %d\n", i);

    return 0;
}



The above approach is easy to understand for beginners. Let us learn some more approaches to copy string. Before learning the optimal method to copy string, lets convert above program using while loop.

Program to copy string using while loop



 
/**
 * C program to copy one string to another string using while loop
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text1[MAX_SIZE];
    char text2[MAX_SIZE];
    int i;
    
    /* Input string from user */
    printf("Enter any string: ");
    gets(text1);
    
    /* Copy text1 to text2 character by character */
    i=0;
    while(text1[i] != '\0')
    {
        text2[i] = text1[i];
        i++;
    }

    //Makes sure that the string is NULL terminated
    text2[i] = '\0';

    printf("First string = %s\n", text1);
    printf("Second string = %s\n", text2);
    printf("Total characters copied = %d\n", i);

    return 0;
}



In C programming, NULL character is represented with 0. Hence, we can embed the string copy logic text2[i] = text1[i] in the while loop condition. Means, you can also write the above while loop as while(text2[i] = text1[++i]);. This will copy characters from text1 to text2 and finally check the current text2 character for NULL. The loop terminates, if current character copied to text2 is NULL.



 
/**
 * C program to copy one string to another string 
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text1[MAX_SIZE], text2[MAX_SIZE];
    int i;
    
    /* Input string from user */
    printf("Enter any string: ");
    gets(text1);
    
    /* Copy text1 to text2 character by character */
    i = -1;
    while(text2[i] = text1[++i]);

    printf("First string = %s\n", text1);
    printf("Second string = %s\n", text2);
    printf("Total characters copied = %d\n", i);

    return 0;
}



Lets go little geeky and apply pointer arithmetic on the above approach.

Program to copy string using pointer



 
/**
 * C program to copy one string to another string using pointer
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char text1[MAX_SIZE], text2[MAX_SIZE];
    char * str1 = text1;
    char * str2 = text2; 
    
    /* Input string from user */
    printf("Enter any string: ");
    gets(text1);
    
    /* Copy text1 to text2 character by character */
    while(*(str2++) = *(str1++));

    printf("First string = %s\n", text1);
    printf("Second string = %s\n", text2);

    return 0;
}



Finally, in real life you can use predefined string library function strcpy(dest-string, source-string) to copy strings. Where dest-string is destination string to which the string is copied and source-string is original string. This function is present in string.h header file.

Program to copy string using strcpy() function



 
/**
 * C program to copy one string to another string using strcpy()
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum size of string

int main()
{
    char text1[MAX_SIZE], text2[MAX_SIZE];

    /* Input original string from user */
    printf("Enter any string: ");
    gets(text1);

    /* Copy text1 to text2 using strcpy() */
    strcpy(text2, text1);

    printf("First string = %s\n", text1);
    printf("Second string = %s\n", text2);

    return 0;
}



Output

Enter any string: c programming
First string = c programming
Second string = c programming
Process returned 0 (0x0) execution time : 6.878 s



Instagram