如果我想知道字符串的长度,而不需要在程序中使用标准库和数组。我该如何找到长度?
最佳答案
您应该迭代 C 字符串,直到到达 \0
#include <stdio.h>
int my_str_len(char *s)
{
int len;
char *ptr = s;
for (len = 0; *ptr; ptr++, len++)
// nothing
;
return len;
}
int main()
{
char str[] = "Hello";
printf("%i\n", my_str_len(str));
return 0;
}
关于c - 如何在不使用 strlen 的情况下找到 C 字符串的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7659004/