C

How To Copy a String in C Using strcpy

In this tutorial, we are going to see how to copy a string in C using strcpy.
 

How To Copy a String in C Using strcpy

strcpy can be used to copy one string into another. Remember that strings in C are arrays of characters. You must pass a string array or a pointer to a string array to this function where the string will be copied.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More The following example will overwrite the contents of str2 with the contents of str1:

#include <stdio.h>
#include <string.h>
 
int main()
{
   char str1[1000], str2[1000];
 
   printf(" Enter a string: ");
   gets(str1);
 
   strcpy(str2, str1);
 
   printf(" Original String: %s\n", str1);
   printf(" Copied String: %s\n", str2);
 
   return 0;
}

Output:

 Enter a string: StackHowTo
 Original String: StackHowTo
 Copied String: StackHowTo

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *