Length of the String in C
Here you will learn and get the program code to find the length of the String in C programming with using strlen() function and without using strlen() function. In this program you will learn to do this program by using 2 different following ways:
1. with strlen() function
2. with user defined function
How to find string length using strlen() function
To find the length of the string, you can use the strlen() builtin function in c programming. Which is part of the string.h library.
Syntax: size_t strlen (const char * str);
For Example:
str = “CodeRevise.com”
length = strlen(str);
Where str is the string whose length is to be found. The strlen() function returns the length = 14 of the string “CodeRevise.com” in terms of number of characters.
Find Length of the String in C with using Strlen
1 2 3 4 5 6 7 8 9 10 11 12 13 | //Find string length using strlen() function #include<stdio.h> #include<string.h> int main() { char st[100]; int len; printf("\n\n\tEnter any string to calculate length\n"); gets(st); len = strlen(st); printf("\n\n\tLength of string is = %d\n",len); return 0; } |
Output
Find String Length without using Strlen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //Find string length Without Using strlen() function #include<stdio.h> #include<string.h> int length(char st[]) { int i; for (i = 0; st[i] != '\0'; i++); return i; } int main() { char st[100]; int len; printf("\n\n\tEnter any string to calculate length :"); gets(st); len = length(st); printf("\n\n\tLength of string is = %d\n",len); return 0; } |
Output
Check out our other C programming Examples