strlen函數也是字元串操作函數,是用來求字元串的長度,以‘\0’為結束标志,長度不包含’\0’這個字元,例如;
#include <stdio.h>
int main()
{
char arr[] = "1234abcd";
printf("%d\n",strlen(arr));
return ;
}
最後程式運作結果如下
現在自己實作my_strlen 函數如下:
#include <stdio.h>
#include <windows.h>
#include <assert.h>
int my_strlen(char * msg)
{
int count = ;
assert(msg);
while (*msg !='\0')
{
count++;//統計字元串的長度
msg++;
}
return count;
}
int main()
{
char msg[] = "1234abcd";
int ret = my_strlen(msg);
printf("%d\n",ret);
system("pause");
return ;
}
當然最後答案是8啦,小夥伴們,是不是很簡單啦,趕緊試一試吧。