C program to find the length of a string – In this article, we will detail in on the means of how to find the length of a string in C programming effectively.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself. In case if you have any doubts related to C program to find the length of the string leave a comment here.
The methods used in this distinct piece are as follows:
- Using Standard Method
- Using Function
- Using Recursion
- Using String Library Function
A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.
As you can see in the image uploaded above, firstly, you need to enter any particular string.
As in this case, the string that has been entered is “kanakesh”
We can see that the length of this string is 8 since it has 8 characters.
THus, the numerous ways to do the same in C programming are as follows:
Using Standard Method – C Program
- The string is nothing but an array of characters.
2) Read the entered string using gets() function and store into the character array s[].
3) The for loop iterates from the first character to the last character of the array. i.e. the last character of the array is null.
4) Print the i value which is the length of the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { char s[1000]; int i; printf("Enter any string: "); gets(s); for(i=0; s[i]!='\0'; i++); printf("Length of '%s' = %d",s,i); return 0; } |
Output:
1 2 |
Enter any string: kanakesh Length of 'kanakesh' = 8 |
Using Function – Find Length Of A String
- The main() function calls the stringlength() function by passing string s as an argument.
2) The function stringlength() has the code to find the length of the string.
For loop iterates with the structure for(i=0;s[i]!=’\0′;i++) upto last character of the array. i.e null.
The function stringlength() returns the i value to main() function.The variable i represents the length of the string.
3) The main() function prints the string and length of the string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int stringlength(char *s) { int i; for(i=0; s[i]!='\0'; i++); return i; } int main() { char s[1000]; int length; printf("Enter any string: "); gets(s); length=stringlength(s); printf("Length of '%s' = %d",s,length); return 0; } |
Output:
1 2 |
Enter any string: goutham Length of 'goutham' = 7 |
Using Recursion
- The main() calls the recursive function stringlength() by passing string s,i=0 as arguments.
2) The recursive function checks the character at s[i] is null or not. If the character is null then it returns the i value.
a) checks the character at s[i] is null or not. If the character is null then it returns the i value.
b) If the character is null then it returns the i value.
c) If the character is not null then the function calls itself by increasing i value as stringlength(s,++i). The function calls recursively until s[i] is null.
The function calls recursively until s[i] is null.
3) Then the main() function prints the i value which is the string length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <stdio.h> int stringlength(char *s,int i) { if(s[i]=='\0') return i; stringlength(s,++i); } int main() { char s[1000]; int length; printf("Enter any string: "); gets(s); length=stringlength(s,0); printf("Length of '%s'= %d",s,length); return 0; } |
Output:
1 2 |
Enter any string: cbeginners Length of 'cbeginners'= 10 |
Using String Library Function
- Here we are using library function strlen() to find the length of the string.The strlen() is available at ‘string.h’ header file.
2) The main() calls the strlen() predefined function by passing string s as an argument. The serlen() calculates the length of the string and the value assigned to the variable length.
3) Print the length value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <stdio.h> #include <string.h> int main() { char s[1000]; int length; printf("Enter any string: "); gets(s); length=strlen(s); printf("Length of '%s'= %d",s,length); return 0; } |
Output:
1 2 |
Enter any string: string length Length of 'string length'= 13 |