hmdb
hmdb

Reputation: 323

strlen() returns the string count with the null terminator

normally, strlen() does not count the null terminator at the end of the string. But, below code prints the string count with the null terminator. Can anyone explain me why? Thanks

char str2[100];
printf("\nEnter a string: ");
fgets (str2, sizeof(str2), stdin);
printf("\n%d",strlen(str2));

Upvotes: 15

Views: 59496

Answers (6)

Arxo Clay
Arxo Clay

Reputation: 876

I am assuming the preceding fgets prompt picked up the newline character.

For example:

You put in apple.

Internally your string was stored as apple\n\0.

strlen then returned 6 for apple + '\n'

Upvotes: 48

anshul garg
anshul garg

Reputation: 503

fgets() reads until \n is encountered.

If the user enters anshul then str2 will contain anshul\n\0.

strlen() will return 7 because strlen() searches until it finds the NULL('\0') character.

Upvotes: 1

midnightCoder
midnightCoder

Reputation: 71

as stated by others, the fgets() will read the newline(\n) character and store it in your array.

after every call to fgets() I always use strcspn() to search the array/pointer to find the newline character and replace it with the null character.

char str2[100];
printf("\nEnter a string: ");
fgets (str2, sizeof(str2), stdin);

//new line of code to replace '\n' with '\0'
str2[strcspn(str2, "\n")] = '\0';

printf("\n%d",strlen(str2));

Upvotes: 2

tonystark
tonystark

Reputation: 97

gets(s) does not include the '\n' when you hit the enter key after being done entering the string.But, fgets() does include the '\n' while reading from a file.

As per the man page(use: man fgets) on linux terminal,

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

Upvotes: 0

The fgets() function accepts the input when a newline character(Enter key when using stdin) is encountered, and the newline character \n is considered a valid character by the function and included in the string copied to your str2.Hence when you pass it as a parameter to strlen() it gives one more than the original number of characters in your string to account for the additional \n character.

If you want the original number of characters or don't want a \n to be added, use the gets() function as it doesn't copy the newline character.And further, you only need to pass the string as argument,no need to pass the stream (stdin) as the default stream for gets() is stdin.

char str2[100];
printf("\nEnter a string: ");
gets(str2);
printf("\n%d",strlen(str2));

Upvotes: 5

user1656808
user1656808

Reputation:

Here you have used fgets() function to take input. When you take input by fgets() function then an additional new line character('\n') will be added with your sting. suppose your input is : "hello" . after typing this sting you must press ENTER key for which new line character will be added with your string. Hence its seems to you that strlen() counts the null terminator. But if you take input using scanf() function it will not add additional new line character('\n') when ENTER is pressed. So you will see the exact number of character you string contains. Run the following code to see my explanation.

#include<stdio.h>
#include<string.h>

void main()
{
char str2[100];
printf("\nEnter a string: ");
scanf("%s",str2);
//fgets (str2, sizeof(str2), stdin);
printf("\n%d",strlen(str2));
}   

Upvotes: 2

Related Questions