Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C strlen()返回带有空终止符的字符串计数_C - Fatal编程技术网

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

C strlen()返回带有空终止符的字符串计数,c,C,通常,strlen()不计算字符串末尾的null终止符。但是,下面的代码使用空终止符打印字符串计数。谁能解释一下原因吗?谢谢 char str2[100]; printf("\nEnter a string: "); fgets (str2, sizeof(str2), stdin); printf("\n%d",strlen(str2)); 我假设前面的fgets提示符拾取了换行符 char str2[100]; printf("\nEnter a string: "); fgets (st

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

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

我假设前面的
fgets
提示符拾取了
换行符

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));
例如:

你把
apple
放进去

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

strlen
然后为
apple
+
'\n'

fgets()
返回
6
,直到遇到
\n
为止

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

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

#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));
}   
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));
#包括
#包括
void main()
{
char-str2[100];
printf(“\n输入字符串:”);
scanf(“%s”,str2);
//fgets(标准文本2,标准文本2,标准文本);
printf(“\n%d”,strlen(str2));
}   
遇到换行符时,
fgets()
函数接受输入(使用
stdin
时按Enter键),并且换行符
\n
被函数视为有效字符,并包含在复制到
str2
的字符串中。因此,当您将其作为参数传递给
strlen时()
它比字符串中的原始字符数多出一个字符来解释额外的
\n
字符

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));
如果您想要原始字符数或不想添加
\n
,请使用
get()
函数,因为它不复制换行符。此外,您只需将字符串作为参数传递,而无需将流(
stdin
)作为
get()的默认流传递
stdin

char str2[100];
printf("\nEnter a string: ");
gets(str2);
printf("\n%d",strlen(str2));
在输入字符串后按enter键时,获取不包括“\n”。但是,fgets()在读取文件时确实包括“\n”

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

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

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

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

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));

它可能是在计算
\n
而不是
\0
,因此您输入了什么字符串,结果是什么?Roger Rowland的可能副本是正确的。“换行符使FGET停止读取,但函数认为它是有效字符,并包含在复制到str的字符串中。”但是编译器警告说GET已经被弃用了,而且很危险。当然,这个问题是五年前提出的,只是说。