C Program to copy one string to another string

Category: C ProgramTags: cprogram string

Write a C program to copy one string to another string using a loop and strcpy function.
We can use the following two methods to copy the string

1. Using loop
2. Using strcpy function

C Program to copy one string to another string using loop

#include <stdio.h>

void main()
{
  char str[50], copy_str[50];
  int i;

  printf("Enter the string: ");
  gets(str);

  for (i = 0; str[i] != '\0'; i++)
    copy_str[i] = str[i];

  copy_str[i] = '\0';

  printf("Copied string: %s", copy_str);
}

C Program to copy one string to another string using strcpy() function

#include <stdio.h>
#include <string.h>

void main()
{
  char str[50], copy_str[50];
  int i;

  printf("Enter the string: ");
  gets(str);

  strcpy(copy_str, str);

  printf("Copied string: %s", copy_str);
}

Output

Output

Enter the string: pro coding
Copied string: pro coding