Find the length of a string without using any loops and string.h in C. Your program is supposed to behave in following way:
Enter a string: GeeksforGeeks (Say user enters GeeksforGeeks) Entered string is: GeeksforGeeks Length is: 13
You may assume that the length of entered string is always less than 100.
The following is the solution.
Entered String is:GeeksforGeeks Length is:13
The idea is to use return values of printf().
printf() returns the number of characters successfully written on output.
In the above program we just use the property of printf() as it returns the number of characters entered in the array string.
Another way of finding the length of a string without using string.h or loops is Recursion.
The following program does the work of finding a length of a string using recursion.
Give a string : Entered string is:0 1
The Function LengthofString calls itself until the character of string is’nt a null character it calls itself, when it calls itself it increases the value of the variable ‘n’ which stores number of times the function has been called and when it encounters the null character the function prints the value of ‘n’ and returns back in the same direction in which it was executed.
This article is contributed by Sumit Singh Chauhan. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print substring of a given string without using any string function and loop in C
- Print 1 to 100 in C++, without loop and recursion
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- How to print a number 100 times without using loop and recursion in C?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- How will you print numbers from 1 to 100 without using loop?
- How to concatenate two integer arrays without using loop in C ?
- How will you print numbers from 1 to 100 without using loop? | Set-2
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- Difference between while and do-while loop in C, C++, Java
- Difference between Sentinel and Counter Controlled Loop in C
- A nested loop puzzle
- Why are elementwise additions much faster in separate loops than in a combined loop?
- C/C++ For loop with Examples
- C/C++ while loop with Examples
- C/C++ do while loop with Examples
- C program to print a string without any quote (singe or double) in the program
- C program to copy string without using strcpy() function
- C program to find the length of a string
- To find sum of two numbers without using any operator