为什么这个计算字符串长度的 C 程序会给出错误的输出?
Why does this C program to calculate string's length give a wrong output?
我写了这个程序,它接受 string
作为输入和 return 它的长度。
#include<stdio.h>
#include<string.h>
#define MAX 100
int main()
{
char a[MAX];
int len;
printf("Enter a string: ");
fgets(a, MAX, stdin);
len = strlen(a);
printf("Length of the string = %d", len);
return 0;
}
既然函数 strlen()
不计算空字符,即 '[=14=]'
,为什么我的输出总是比输入 string
的字符多 1?
例如-
Enter a string: Aryan
Length of the string = 6
Process returned 0 (0x0) execution time: 4.372 s
Press any key to continue.
如果提供的数组中有 space,函数 fgets
可以将换行符 '\n'
添加到输入的字符串中。
来自C标准(7.21.7.2 fgets函数)
2 The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream into
the array pointed to by s. No additional characters are read after a
new-line character (which is retained) or after end-of-file. A null
character is written immediately after the last character read into
the array
因此在 strlen
的调用中
len = strlen(a);
换行符也算在内
您需要将其删除,例如
a[ strcspn( a, "\n" ) ] = '[=11=]';
或
char *p = strchr( a, '\n' );
if ( p != NULL ) *p = '[=12=]';
fgets()
调用包括从输入流中读取的换行符。这很有用,因为它允许您检查该行是否已被完整读取。如果输入的最后一个字符没有换行符,则读取的行不完整。
int main()
{
char a[MAX];
size_t len; // being precise with your types is a good habit
printf("Enter a string: ");
fgets(a, MAX, stdin);
len = strlen(a);
if ( a[len - 1] == '\n' )
{
// Usually you don't want the newline in there.
a[--len] = '[=10=]';
// --len above corrected the length.
printf("Length of the string = %zu\n", len);
}
else
{
printf("Length of the string longer than %d\n", MAX - 1);
// You can repeat fgets() until you get the whole line,
// or keep reading (and throwing away) from input until
// you get a newline. It really depends on how you want
// to handle the error.
}
return 0;
}
我写了这个程序,它接受 string
作为输入和 return 它的长度。
#include<stdio.h>
#include<string.h>
#define MAX 100
int main()
{
char a[MAX];
int len;
printf("Enter a string: ");
fgets(a, MAX, stdin);
len = strlen(a);
printf("Length of the string = %d", len);
return 0;
}
既然函数 strlen()
不计算空字符,即 '[=14=]'
,为什么我的输出总是比输入 string
的字符多 1?
例如-
Enter a string: Aryan
Length of the string = 6
Process returned 0 (0x0) execution time: 4.372 s
Press any key to continue.
如果提供的数组中有 space,函数 fgets
可以将换行符 '\n'
添加到输入的字符串中。
来自C标准(7.21.7.2 fgets函数)
2 The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array
因此在 strlen
len = strlen(a);
换行符也算在内
您需要将其删除,例如
a[ strcspn( a, "\n" ) ] = '[=11=]';
或
char *p = strchr( a, '\n' );
if ( p != NULL ) *p = '[=12=]';
fgets()
调用包括从输入流中读取的换行符。这很有用,因为它允许您检查该行是否已被完整读取。如果输入的最后一个字符没有换行符,则读取的行不完整。
int main()
{
char a[MAX];
size_t len; // being precise with your types is a good habit
printf("Enter a string: ");
fgets(a, MAX, stdin);
len = strlen(a);
if ( a[len - 1] == '\n' )
{
// Usually you don't want the newline in there.
a[--len] = '[=10=]';
// --len above corrected the length.
printf("Length of the string = %zu\n", len);
}
else
{
printf("Length of the string longer than %d\n", MAX - 1);
// You can repeat fgets() until you get the whole line,
// or keep reading (and throwing away) from input until
// you get a newline. It really depends on how you want
// to handle the error.
}
return 0;
}