返回字符串不包括"\0"的長度。
下面是解釋:
NAME
?????? strlen - calculate the length of a string
SYNOPSIS
?????? #include <string.h>
?????? size_t strlen(const char *s);
DESCRIPTION
?????? The? strlen()? function? calculates? the? length? of? the string s, not
?????? including the terminating `\0' character.
在unix中,讀入一行時都會在后面加入"\n"緊跟一個"\0",所以strlen往往得不到正確的答案。應該是strlen(s) - 1
所以清除換行符的方法是
char test[MAX_LINE];
//....read from stdin
test[strlen(test) - 1] = '\0';