Javadream

          A long way and a dream.

          strlen(char *s)

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

          主站蜘蛛池模板: 长丰县| 兴隆县| 石屏县| 富蕴县| 南昌市| 容城县| 扎赉特旗| 东宁县| 湘阴县| 壤塘县| 佛冈县| 景德镇市| 辰溪县| 巨野县| 清苑县| 冕宁县| 定襄县| 共和县| 拉孜县| 铜梁县| 九寨沟县| 江安县| 昆山市| 井冈山市| 湾仔区| 安图县| 苍溪县| 务川| 临邑县| 双桥区| 剑川县| 沙雅县| 读书| 龙游县| 泰宁县| 周宁县| 白山市| 安泽县| 手机| 凭祥市| 江西省|