C program to calculate length of the string using recursion

In this program, we will read a string through user and calculate the length of the string using recursion in c programming language. We will not use standard function strlen() to calculate the length of the string.

Length of the string program using recursion

/*function to calculate length of the string using recursion.*/
 
#include <stdio.h>
 
//function to calculate length of the string using recursion
int stringLength(char *str)
{
    static int length=0;
    if(*str!=NULL)
    {
        length++;
        stringLength(++str);
    }
    else
    {
        return length;
    }
}
int main()
{
    char str[100];
    int length=0;
     
    printf("Enter a string: ");
    gets(str);
     
    length=stringLength(str);
     
    printf("Total number of characters (string length) are: %d\n",length);
 
    return 0;
}

Output

Enter a string: www.includehelp.com
Total number of characters (string length) are: 19

C Recursion Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.