This C Program uses recursive function & calculates the length of a string. The user enters a string to find it’s length.
Here is the source code of the C program to find the length of a string. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.
* C program to find the length of a string
int length(char , int);
printf("Enter a word to count it's length: ");
count = length(word, 0);
printf("The number of characters in %s are %d.\n", word, count);
int length(char word, int index)
if (word[index] == '\0')
return (1 + length(word, index + 1));
$ cc pgm17.c $ a.out Enter a word to count it's length: 5 The number of characters in 5 are 1. $ a.out Enter a word to count it's length: sanfoundry The number of characters in sanfoundry are 10.
Sanfoundry Global Education & Learning Series – 1000 C Programs.
If you wish to look at other example programs on Linked List, go to Linked List. If you wish to look at programming examples on all topics, go to C Programming Examples.