Header Ads

17 - WAP to Implement own strlen() function without using strlen() in C

This program prints the Length of String by function without using strlen().

  • This code implements a own strlen() function to calculate the length of a given string without using strlen()
/* to print Length of String by function without using strlen() */
#include 
/* Function for String Length  */ 
int len(const char *str)
{
    int count=0;
    while(*str!='\0')
    {
        count++;
        str++;
    }
    return(count);
}

/* main program function */
void main()
{
    char str[100];
    int length = 0;
    printf("\n Enter any String : ");
    gets(str);
    /* Own string length function call */
    length = len(str);
    printf(" Length of String is = %d \n\n", length);
}
  
Program Output:

No comments