Saturday, 28 April 2012

C Program to Copy a String using Pointers


#include<stdio.h>
#include<conio.h>

void copy(char*, char*);

int main()
{
    char a[100], b[100];

    clrscr();
    printf("Enter the source string\n");
    gets(a);

    copy(b, a);

    printf("The target string is \"%s\"\n", b);

    getch();
    return 0;
}

void copy(char *b, char *a)
{
   while(*a)
   {
      *b = *a;
      a++;
      b++;
   }
   *b = '\0';
}

No comments:

Post a Comment