This blog is under construction

Monday 24 June 2013

C program to copy one string to another without using strcpy

Write a C program to copy one string to another without using library function(strcpy).


  #include <stdio.h>
  int main() {
        char str1[100], str2[100], i = 0;

        /* get the input from the user */
        printf("Enter your input:");
        fgets(str1, 100, stdin);

        /* copy the given string to another */
        while (str1[i] != '\n' && str1[i] != '\0') {
                str2[i] = str1[i];
                i++;
        }
        str1[i] = str2[i] = '\0';

        /* print the results */
        printf("Original string: %s\n", str1);
        printf("Copied string: %s\n", str2);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter your input : Hello world
  Original string    : Hello world
  Copied string     : Hello world



No comments:

Post a Comment