#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;
}
Master programming effortlessly with Simplest Codings. From core algorithms and LLMs to clean code in Java, C++, and Python—get clear, bite-sized tutorials and practical developer guides for all skill levels.
Monday, August 16, 2010
Custom strncpy function in C
Here's a simple implementation of a custom desired length string copy function. This function copies a string into another upto a desired length.
Subscribe to:
Post Comments (Atom)
-
Overview of the SSL handshake Courtesy: http://www-01.ibm.com/support/knowledgecenter Steps involved in SSL handshake (Courtesy:...
-
To implement the kind of storage which stores strings as the search keys , there is a need to have special data structures which can store ...
-
A simple implementation of a packet sniffer in C on linux platform using the libpcap library. This packet sniffer currently sniffs IP , TCP ...
No comments:
Post a Comment
Please post your valuable suggestions