C strcpy

The strcpy() function is used to copy strings. It copies string pointed to by source into the destination  . This function accepts two arguments of type pointer to char or array of characters and returns a pointer to the first string i.e destination.

Syntax:

 strcpy(destination,source);

Example 

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10];
    char str2[10];
    char str3[20];
    printf("Enter the Source String:");
    gets(str1);
    strcpy(str2, str1);
    printf("\n The destination string is:");
    puts(str2);
    return 0;

Output



 

Copy String Without Using strcpy()
 
#include<stdio.h>
 
int main() {
   char str1[100], str2[100];
   int i;
 
   printf("\nEnter the string :");
   gets(str1);
 
   i = 0;
   while (str1[i] != '\0') {
      str2[i] = str1[i];
      i++;
   }
 
   str2[i] = '\0';
   printf("\nCopied String is %s ", str2);
 
   return (0);
}

 Output

 

 

No comments:

Post a Comment