Signup/Sign In

C Program to find the Length of the String using Recursion

learn c language tutorial

Logic To Find The Length Of The String:

  • Here the main function calls back the recursive function, by passing the arguments,
  • The character str is checked whether it is null or not,
  • If the character has no value it will return the 'i' value,
  • In case the value is not 0, Then the function will be called by incrementing the value of 'i',
  • The function is called until the value is 0,
  • main() func returns the value of the length of the string.

C Program To Calculate The Length Of The String Using Recursion:

int length(char *str,int i)
{
    if(str[i]=='\0')
     return i;
    length(str,++i);
	
}
int main()
{
    char str[50];  
    int find;
 
    printf("Enter The String To Find Length: ");
    gets(str);
    find=length(str,0);
    printf("Length Of The Given String Is'%s'= %d",str,find);
 
    return 0;
}

Output:

Find Length