Friday, June 20, 2014

Function to copy one string into another using pointers

To copy one string to another string, we have built-in function defined in <string.h> library but to be a good programmer, we should have knowledge about the algorithm of function and the story of back end code. The following code is the implementation of function to copy one string into another using pointers in C Programming Language.


#include<stdio.h>
void strcp(char *a , char *b)
{
    int i;
    for(i=0;*b!='\0';i++)
    {
        *a=*b;
         a++;
         b++;

    }
}
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}
int main()
{
    char a[100];
    char b[100];
    header();
    system("pause");
    system("cls");
    puts("Enter String 1: ");
    gets(a);
    puts("Enter String 2 : ");
    gets(b);
    strcp(a,b);
    printf("\nAfter Copying String 2 in String 1 \n\n");
    printf("String 1 is : %s \n\n\n\n\n",a);
}

2 comments: