- Get link
- X
- Other Apps
Featured Post
Posted by
Unknown
on
- Get link
- X
- Other Apps
Here's a simple implementation of a custom desired length string copy function. This function copies a string into another upto a desired length.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *source;
char *target;
char *ret;
int no_of_char;
/*function prototype it takes string and returns address*/
char *my_strncpy(char *target1,const char *source1,int no);
source=(char*)malloc(20);
target=(char*)malloc(20);
printf("enter the string to be copied:\n");
fgets(source,20,stdin); //taking input from user
printf("\nenter the number of characters to be copied:");
scanf("%d",&no_of_char);
ret=my_strncpy(target,source,no_of_char);
printf("\nThe address of copy of string is %d ",ret ); //print the address of copt of string
printf("\nThe copied string is %s \n", ret); //print the copy of string
free(source);
free(target);
return EXIT_SUCCESS;
}
char * my_strncpy(char *target1,const char *source1,int no)
{
char *tempt=target1;
int n=0;
while(n<no)
{
*target1=*source1;
target1++;
source1++;
n++;
}
*target1='\0';
return tempt;
}
Comments
Post a Comment
Please post your valuable suggestions