求字串長度

2021-08-13 19:13:23 字數 728 閱讀 6501

目的:考察對strlen函式的使用

功能:求字串長度,統計並返回字串中第乙個『\0』之前的有效字元的個數,注意不包括』\0』占用的位元組數。

strlen函式的常規寫法。

#include

#include

int main()

拓展:模擬實現strlen函式,求字串長度。

『\0』是最關鍵的部分。

#include

#include

int my_strlen(const char *str)//const放在星號的左邊表示指標所指向的內容不能通過指標改變,這樣使**更加安全

return

count;

}int main()

深度拓展:不允許建立臨時變數求字串長度(遞迴寫法

#includeint my_strlen(const

char *str)//const放在星號的左邊表示指標所指向的內容不能通過指標改變,這樣**更加安全

else

}int main()

求字串長度

問題及 檔名稱 main.cpp 作 者 王藝霖 完成日期 2016年3月20日 版 本 號 v1.0 問題描述 寫乙個函式,求乙個字串的長度,在main函式中輸出字元,並輸出其長度 輸入描述 輸入一行字串 輸出描述 輸出字串長度 includeusing namespace std int str...

求字串長度

思路分析 迴圈法 呼叫函式傳參,迴圈條件為判斷字元陣列元素是否為真,若為真,引數加1,若為假返回0 遞迴法 遞推關係 1 strlen str 1 指標加1 出口 指標指向 0 完整 define crt secure no warnings include include include incl...

shell求字串長度

string 的長度 在 string中,從位置 position 開始提取子串 在 string中,從位置 position 開始提取長度為 length 的子串 在shell 中,通過 awk,sed,expr 等都可以實現,字串上述操作。下面我們進行效能比較。chengmo localhost...