strlen函數(shù)也是字符串操作函數(shù),是用來求字符串的長(zhǎng)度,以‘/0’為結(jié)束標(biāo)志,長(zhǎng)度不包含’/0’這個(gè)字符,例如;
#include <stdio.h>int main(){ char arr[9] = "1234abcd"; 最后程序運(yùn)行結(jié)果如下
現(xiàn)在自己實(shí)現(xiàn)my_strlen 函數(shù)如下:
#include <stdio.h>#include <windows.h>#include <assert.h>int my_strlen(char * msg){ int count = 0; assert(msg); while (*msg !='/0') { count++;//統(tǒng)計(jì)字符串的長(zhǎng)度 msg++; } return count;}int main(){ char msg[] = "1234abcd"; int ret = my_strlen(msg); printf("%d/n",ret); system("pause"); return 0;}當(dāng)然最后答案是8啦,小伙伴們,是不是很簡(jiǎn)單啦,趕緊試一試吧。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注