How to find length of a string without string.h and loop in C?
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 solution.
#include <stdio.h> int main() { char str[100]; printf("Enter a string: \n"); gets(str); printf("Entered string is:%s\n", str); printf( "\rLength is: %d",strlen(str)); return 0; } |
Output:
Enter a string: GeeksforGeeks Entered string is: GeeksforGeeks Length is: 13
The idea is to use return values of printf() and gets().
gets() returns the enereed string.
printf() returns the number of characters successfully written on output.
In the above program, gets() returns the entered string. We print the length using the first printf. The second printf() calls gets() and prints the entered string using returned value of gets(), it also prints 20 extra characters for printing “Entered string is: ” and “\n”. That is why we subtract 20 from the returned value of second printf and get the length.
This article is contributed by Umesh Sharma. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
#include <stdio.h> void LengthofString(int n,char *string) { if(string[n] == '\0') { printf("%i",n); return; } LengthofString(n+1,string); //printf("%c",string[n]); } int main() { char string[100]; printf("Give a string : \n"); scanf("%s",string); printf("Entered string is:%s\n", string); LengthofString(0,string); return 0; } |
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.
Recommended Posts:
- C program to find the length of a string
- Print substring of a given string without using any string function and loop in C
- The length of a string using pointers
- Counts of distinct consecutive sub-string of length two using C++ STL
- A nested loop puzzle
- How will you print numbers from 1 to 100 without using loop?
- Difference between while and do-while loop in C, C++, Java
- How will you print numbers from 1 to 100 without using loop? | Set-2
- Print 1 to 100 in C++, without loop and recursion
- How to concatenate two integer arrays without using loop in C ?
- What happens if loop till Maximum of Signed and Unsigned in C/C++?
- How to print a number 100 times without using loop and recursion in C?
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
Improved By : Zeeking99



