Easy Tutorial
❮ C Nested Switch C Exercise Example95 ❯

C Language Example - String Copy

C Language Examples

Copy a string from one variable to another.

Example - Using strcpy()

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

int main()
{
   char src[40];
   char dest[100];

   memset(dest, '\0', sizeof(dest));
   strcpy(src, "This is tutorialpro.org");
   strcpy(dest, src);

   printf("Final destination string: %s\n", dest);

   return(0);
}

Output:

Final destination string: This is tutorialpro.org

Example - Without Using strcpy()

#include <stdio.h>

int main()
{
    char s1[100], s2[100], i;

    printf("String s1: ");
    scanf("%s",s1);

    for(i = 0; s1[i] != '\0'; ++i)
    {
        s2[i] = s1[i];
    }

    s2[i] = '\0';
    printf("String s2: %s", s2);

    return 0;
}

Output:

String s1: tutorialpro
String s2: tutorialpro

C Language Examples

❮ C Nested Switch C Exercise Example95 ❯