C Program Find the Length of Linked List using Recursion

«
»
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.

  1. /*
  2.  * C program to find the length of a string
  3.  */
  4. #include <stdio.h>
  5.  
  6. int length(char [], int);
  7. int main()
  8. {
  9.     char word[20];
  10.     int count;
  11.  
  12.     printf("Enter a word to count it's length: ");
  13.     scanf("%s", word);
  14.     count = length(word, 0);
  15.     printf("The number of characters in %s are %d.\n", word, count);
  16.     return 0;
  17. }
  18.  
  19. int length(char word[], int index)
  20. {
  21.     if (word[index] == '\0')
  22.     {
  23.         return 0;
  24.     }
  25.     return (1 + length(word, index + 1));
  26. }

$ 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.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.