20130505

How to copy a string without using strcp with pointer

/*How to copy a string without using library function strcpy.
How to use pointer/array to copy a string/sentence. C++/CPP/C program/code to copy a string/word/sentence without using inbuilt function strcpy.How copy a string without using library function strlen. cpp program to copy a string using pointer.cpp program to copy a string without using library function strcpy*/
#include<stdio.h>
#include<string.h>

void strCpy(char *p2,char *p1);

int main()
{
char arr1[20];
char arr2[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

strCpy(arr2, arr1);

printf("You entered and copied:%s\n",arr2);


return 0;
}
void strCpy(char *p2,char *p1)
{
while(*p1!='\0')
{
*p2++=*p1++;
}
*p2='\0';
}
/*
* Enter a String:
India
You entered and copied:India
*/

/*cpp program for copying a string without using library function strcpy*/
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[20];
printf("Enter a String:\n");
scanf("%s",&arr1);

char arr2[20];
int i,j;
for(i=0,j=0;arr1[i]!=0;i++,j++)
{
arr2[j]=arr1[i];
}
arr2[j]='\0';
printf("You entered and copied:%s\n",arr2);

return 0;
}
/*
Enter a String:
Hello
You entered and copied:Hello
*/


Previous         Home           Next

1 comment:

  1. #include
    using namespace std;
    #include
    int main()
    {
    char sourceString[]="howtocpp";
    char destination[20];
    strcpy(destination,sourceString);
    cout<<"sourceString ie howtocpp is copied to destination."<<endl;
    return 0;
    }
    //sourceString ie howtocpp is copied to destination.

    ReplyDelete