C Program to find length of string using recursion

Simple C program to find length of string using recursion: We know that at the end of string there is a null character and null character is denoted by '\0'. So if pointer to string reaches to end of string then function return value 0.
/* C program to find length of string using recursion */
#include<stdio.h>
int StringLength(char *str)
{ 
 if(*str=='\0')//Null character at the end of string
 return 0;
 return 1+StringLength(str+1);
}
int main()
{
 char str[16]="C Basic Examples";
 int len=StringLength(str);
 printf("String length = %d",len);
 return 0;
}
/* End of the program */


Popular posts from this blog