gets() and strlen() Function Examples in C Language
Let's start by learning about gets()
and strlen()
functions in C. These functions are used for managing strings in C language.
What is gets() function in C?
The syntax of gets()
is as follows:
char * gets(char * str);
It reads characters from the standard input (stdin) and stores them as a C string into str
until a newline character or the end-of-file is reached. The newline character, if found, is not copied into str
.
A terminating null character is automatically appended after the characters which are copied to str
.
It has str
as a parameter which is pointer to a block of memory (array of char) where the string read is copied as a C string.
On success, the function returns str
.
If the end-of-file is encountered while attempting to read a character, the eof indicator is set (feof). If this happens before any characters could be read, the pointer returned is a null pointer (and the contents of str remain unchanged). In any case of error, a null pointer in returned.
What is strlen() function in C?
The syntax of strlen()
is as follows:
size_t strlen(const char * str);
It returns the length of the C string str
. When it encounters a null-character, it terminates the string.
It take a C string as a parameter.
1. C Program to find Length of a string using strlen()
function
Let's see the steps followed,
-
Declare a char array according to length of the string you want to input.
-
Read input in the string using gets()
.
-
Calculate length of the string using strlen()
and copy the value returned by it in a variable named length.
-
Print length of the string.
#include<stdio.h>
#include<string.h>
int main()
{
printf("Studytonight - Best place to learn\n");
char a[100];
int length;
printf("Enter a string you wish to calculate the length of : ");
/*
to take a single string (including spaces)
as input at a time
*/
gets(a);
/*
Return the length of the string
or the number of characters in the string
*/
length = strlen(a);
printf("The length of the input string is: %d", length);
printf("\nCoding is Fun !");
return 0;
}
Studytonight - Best place to learn
Enter a string you wish to calculate the length of : studytonight
The length of the input string is: 12
Coding is Fun !
Note: While compiling the above code, we get a warning that gets()
is dangerous and should not be used. The use of gets()
is depreciated. It is so because the function doesn't know how big the buffer is, so it continues reading until it finds a newline or encounters EOF, and may overflow the bounds of the buffer it was given. In order to use it safely, we have to know exactly how many characters we will be reading, so that we can make your buffer large enough. We will only know that if we know exactly what data we will be reading.
The first internet worm used gets()
to propagate from system to system.
Some important points about gets()
and scanf()
methods are:
-
Both scanf()
and gets()
are used to take input from user.
-
scanf()
skips the input string after the first space is entered.
-
gets()
is used to input a complete sentence with any desired number of spaces until the Enter key is pressed.
2. C Program to find Length of a string without strlen()
function
Before moving on to the code, remember that a string is terminated by a null character. So, the last character in every string is '\0'.
Let's see the steps followed,
-
Declare a char
array according to length of the string you want to input.
-
Read input in the string using gets()
.
-
To calculate length of the string, we will iterate over the string till we reach the null character. We will use a for loop to accomplish this.
-
Print length of the string.
Below is a program to find the length of a string without the use of strlen()
function.
#include<stdio.h>
int main()
{
printf("Studytonight - Best place to learn\n");
char a[1000];
int i = 0;
printf("Enter the string: ");
gets(a);
// \0 marks the end of the string
for(i = 0; a[i] != '\0' ;i++); // updation is done after checking condition
printf("The length of the input string is : %d", i);
printf("\nCoding is Fun !");
return 0;
}
Studytonight - Best place to learn
Enter the string: abcdefghi
The length of the input string is : 9
Coding is Fun !
Explanation:
You must be wondering why we have not added any statement to our for
loop. It's because we are traversing our string until we encounter \0
and are incrementing i
every time, hence we do not have to do anything else inside the loop, as the value of i
will be equal to the length of the string by the time we encounter the \0
character.