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.
/*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
Length of the string program using recursion
Output
need an explanation for this answer? contact us directly to get an explanation for this answer