Javadream

          A long way and a dream.

          strlen(char *s)

          Posted on 2006-07-19 21:39 oxl 閱讀(302) 評論(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';

          主站蜘蛛池模板: 阳春市| 永新县| 西城区| 邢台县| 东宁县| 绵阳市| 台南县| 密云县| 阿城市| 锡林郭勒盟| 宣恩县| 洪江市| 商城县| 饶河县| 清河县| 资中县| 镇江市| 白银市| 合阳县| 肥东县| 伊宁市| 周宁县| 乌恰县| 瑞金市| 呈贡县| 门头沟区| 无棣县| 巴南区| 乌苏市| 永修县| 玉林市| 平定县| 凤台县| 娄底市| 南澳县| 周口市| 长兴县| 五台县| 尼勒克县| 兴海县| 嘉黎县|