C Program to Copy String Without Using strcpy()

Copy string in C without using strcpy

#include <stdio.h>
#include <string.h>
     int main()
     {
         char source[1000], destination[1000];
         int c=0;
         printf("Input a string\n");
         gets(source);
          while (source[c] != '\0')
          {
                destination[c] = source[c];
                c++;
            }
        destination[c] = '\0';
         printf("Source string: %s\n", source);
         printf("Destination string: %s\n", destination);
         return 0;

     } 

Creating a function to copy a string

     #include <stdio.h>
     #include <string.h>
     void copy_string(char [], char []);
     int main()
     {
         char source[1000], destination[1000];

         printf("Input a string\n");
         gets(source);
         printf("Source string: %s\n", source);
         copy_string(destination, source); /*Function call*/
         printf("Destination string: %s\n", destination);
         return 0;
         }
         void copy_string(char destination[], char source[])/*Function Defination*/
         {
            int c=0;
          while (source[c] != '\0')
          {
                destination[c] = source[c];
                c++;
            }

        destination[c] = '\0';


     }

Run:
 

No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...