如何在不使用循环和String类函数(如strlen())的情况下找出字符串的长度?
答案 0 :(得分:4)
后续if
s?
if (str[0] == 0) return 0;
if (str[1] == 0) return 1;
///keep going until fingers start bleeding
答案 1 :(得分:3)
int length (const char* p)
{
return *p == 0 ? 0 : 1 + length(p+1);
}
任何迭代函数理论上都可以递归实现,反之亦然。
答案 2 :(得分:-1)
#include <stdio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
}