Length of string in C is a program to find length of string in c.
In this tutorial we are going to learn new one of the important and simple program which is to find length of the string in c.
Before seeing program to find length of string in c first we will see string in c
String in c
String in c is a collection of character. And it ends with the null character.
In c string has different string function such as strcmp, strrev, strlen etc.
String length in c
To find the length of the string in c the string class provided one function strlen () using this function we can find length of the string.
For using this function we need to include string.h header file.
Syntax for String length
strlen in c to find length of string in c.
int len = strlen(string);
This function returns an integer value which is a length of the string.
Strlen example in c :-
The length of sting is 9
Logic to find length of string in c
First we declare string.h header file. Next week take input string from user. Now using the strlen() find length of string. Print the length of the string.
Algorithm to find length of string in c
2. Declare variable
3. a string input from user
4. length of the string using string function
5. Print the length of the string
C program to find length of the string using strlen in c.
printf("Enter a string to know the length\n");
printf("The length of %s is %d\n",str,len);
1. Write a header files stdio.h and string.h as we are using string function.
2. Next write the main function from where the execution of program starts.
3. Declare the variables
str => to store the string
len => to store length of string
4. Next take a input string from user.
printf(“Enter a string to know the length\n”);
We use a gets() to take a input string from user.
Difference between scanf and gets is scanf don’t accept space in string but gets accepts space in string.
5. Example :-
str = technical
6. Next we find length of the string using string function
len=strlen(str) => strlen finds the length of the string
=> len = 9
7. Next we print the length of string
printf(“The length of %s is %d\n”,str,len);
=> The length of technical is 9