Q:

C program to calculate length of the string using recursion

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to reverse an integer number using recur... >>
<< C program to calculate sum of all digits using rec...