方式一:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char str[] = "hello\0world";
int len = 0;
while (str[len] != '\0')
{
len++;
}
printf("字符串的实际长度为:%u\n", len);
system("pause");
return EXIT_SUCCESS;
}
方式二(指针):
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int my_Strlen(char arr[])
{
char* p = arr;
while (*p != '\0')
{
p++;
}
return (p - arr);
}
int main()
{
char arr[] = "hello myhoney";
int ret = my_Strlen(arr);
printf("%d\n", ret);
system("pause");
return EXIT_SUCCESS;
}