Q15 Write your own C program of strlen(), strcpy(), strncpy() and strcmp()

Interview can ask write your own program or user defined function of strlen(), strcpy(), strncpy().

User defined function of strlen()

strlen is used to find the length of a string.

Prototyoe of strlen() function :  size_t  strlen(const char * str);

Program of strcpy()

#include <stdio.h>

int len(const char *str)

{

    int count=0;

    while(*str!=’\0′)

    {

        count++;

        str++;

    }

    return(count);

}

void main()

{

    char str[25];

    int length = 0;

    printf(“Enter any string = “);

    gets(str);

    /* Own string length function call */

    length = len(str);

    printf(“length of string is = %d”,length);

}

Output:

$ ./a.exe

Enter any string = gyantoday.com

length of string is = 13

 

Try to write user defined function of strcpy(), strncpy and strcmp() by yourself.

User defined function of strcpy()

User defined function of strncpy()

User defined function of strcmp()

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.