Javadream

A long way and a dream.

strlen(char *s)

Posted on 2006-07-19 21:39 oxl 阅读(280) 评论(0)  编辑  收藏 所属分类: 技术感语

返回字符串不包括"\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';


只有注册用户登录后才能发表评论。


网站导航: