Hello friends,
Iam implementing my own strcpy function
following is the code.
Code:
#include <stdio.h>
void myStrCpy(char *pDstStr,char *pSrcStr);
void myStrCpyTD();
void myStrCpyTD()
{
char str[100];
char tempStr[100];
char *p;
memset(str,sizeof(str),0);
memset(tempStr,sizeof(tempStr),0);
fflush(stdin);
while(fgets(str,sizeof(str),stdin) != NULL)
;
if ((p = strchr(str,'\n')) != NULL) ;
*p = '\0';
myStrCpy(tempStr,str);
printf("copied for dst from src and Dst is %s \n",tempStr);
}
void myStrCpy(char *pDstStr,char *pSrcStr)
{
while (*pSrcStr != '\0')
{
*pDstStr = *pSrcStr;
pSrcStr++;
pDstStr++;
}//while
pDstStr = '\0';
printf("%s",pDstStr);
}
int main()
{
int choice = 0;
do
{
printf("3.Copy form Src to Dst\n");
printf("6.To Exit \n");
scanf("%d",&choice);
switch(choice)
{
case 3:
myStrCpyTD();
break;
case 6:
printf("Coming out the program \n");
break;
default:
printf("Please choose the valid option \n");
break;
}//switch
}while (choice != 6); //while
return 0;
}
Output
# ./ex6.o
3.Copy form Src to Dst
6.To Exit
3
John
(null)copied for dst from src and Dst is John¡,î¿
3.Copy form Src to Dst
6.To Exit
6
Coming out the program
1) The bug iam seeing is while printing the tempStr it contains some other junk characters
also
2) why cannot i print the the string with pDstStr in the myStrCpy function.