-6

我如何在不使用循环和字符串类函数(如 strlen() )的情况下找出字符串的长度?

4

3 回答 3

4

后续ifs?

if (str[0] == 0) return 0;
if (str[1] == 0) return 1;
///keep going until fingers start bleeding
于 2013-02-04T14:32:17.183 回答
3
int length (const char* p) 
{
    return *p == 0 ? 0 : 1 + length(p+1);
}

任何迭代函数理论上都可以递归实现,反之亦然。

于 2013-02-04T18:23:53.933 回答
-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);
}
于 2013-03-27T17:37:34.697 回答