C语言 strlen() 返回带有空终止符的字符串计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16314468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:13:57  来源:igfitidea点击:

strlen() returns the string count with the null terminator

c

提问by hmdb

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

通常, strlen() 不计算字符串末尾的空终止符。但是,下面的代码使用空终止符打印字符串计数。谁能解释我为什么?谢谢

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

回答by Arxo Clay

I am assuming the preceding fgetsprompt picked up the newlinecharacter.

我假设前面的fgets提示选择了newline字符。

For example:

例如:

You put in apple.

你放进去apple

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

在内部,您的字符串存储为apple\n\0.

strlenthen returned 6for apple+ '\n'

strlen然后返回6apple+'\n'

回答by Rüppell's Vulture

The fgets()function accepts the input when a newline character(Enter key when using stdin) is encountered, and the newline character \nis 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 \ncharacter.

fgets()当遇到换行符(使用时 Enter 键stdin)时,该函数接受输入,并且该换行符\n被函数视为有效字符并包含在复制到您的字符串中 str2。因此,当您将其作为参数传递给strlen()它时比字符串中的原始字符数多 1 以说明额外的\n字符。

If you want the original number of characters or don't want a \nto 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.

如果您想要原始字符数或不想\n添加 a,请使用该gets()函数,因为它不会复制换行符。此外,您只需将字符串作为参数传递,无需传递流( stdin) 作为gets()is的默认流stdin

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

回答by midnightCoder

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

正如其他人所说, fgets() 将读取换行符 (\n) 并将其存储在您的数组中。

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.

每次调用 fgets() 后,我总是使用 strcspn() 搜索数组/指针以查找换行符并将其替换为空字符。

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

//new line of code to replace '\n' with '
#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));
}   
' str2[strcspn(str2, "\n")] = '##代码##'; printf("\n%d",strlen(str2));

回答by midnightCoder

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.

在这里,您使用了 fgets() 函数来获取输入。当您通过 fgets() 函数获取输入时,将在您的字符串中添加一个额外的换行符 ('\n')。假设您的输入是: "hello" 。键入此字符串后,您必须按 ENTER 键,将为您的字符串添加新行字符。因此,在您看来, strlen() 计算空终止符。但是,如果您使用 scanf() 函数进行输入,则在按下 ENTER 时不会添加额外的换行符 ('\n')。因此,您将看到字符串包含的确切字符数。运行下面的代码看我的解释。

##代码##

回答by anshul garg

fgets()reads until \nis encountered.

fgets()读取直到\n遇到。

If the user enters anshulthen str2will contain anshul\n\0.

如果用户输入anshul,然后str2将包含anshul\n\0

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

strlen()将返回 7,因为strlen()搜索直到找到 NULL('\0') 字符。

回答by tonystark

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.

在完成输入字符串后按回车键时,gets(s) 不包含 '\n'。但是,fgets() 在读取文件时包含 '\n'。

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

根据 linux 终端上的手册页(使用:man fgets),

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.

fgets() 从流中读取至多小于 size 的字符,并将它们存储到 s 指向的缓冲区中。阅读在 EOF 或换行符后停止。如果读取换行符,则将其存储到缓冲区中。终止空字节 ('\0') 存储在缓冲区中的最后一个字符之后。