C Program to Find the Length of the String

This C Program uses recursive function & counts the number of nodes in a linked list. A linked list is an ordered set of data elements, each containing a link to its successor.

Here is the source code of the C program to count the number of nodes in a linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * Recursive C program to find length of a linked list
  3.  */
  4. #include <stdio.h>
  5.  
  6. int find_len (char [], int);
  7.  
  8. int main ()
  9. {
  10. 	char str[100];
  11. 	int len = 0;
  12.  
  13. 	printf ("Enter the string: \n");
  14. 	scanf ("%[^\n]s", str);
  15.  
  16. 	len = find_len (str, 0);
  17.  
  18. 	printf ("The length of the given string is: %d\n", len);
  19. 	return 0;
  20. }
  21.  
  22. int find_len (char str[], int index)
  23. {
  24. 	static int l = 0;
  25.  
  26. 	if (str[index] == '\0')
  27. 		return l;
  28. 	else
  29. 		l ++;
  30.  
  31. 	find_len (str, index + 1);
  32. }

Enter the string:
Sanfoundry C Programming
The length of the given string is: 24
 
Enter the string:
Programming Examples
The length of the given string is: 20

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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 Strings, go to C Programming Examples on Strings. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.