C Program to copy one string to another 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
1. 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);
}
2. 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
Enter the string: pro coding
Copied string: pro coding