Which of the following can be the base case for the recursive implementation used to find the length of a string? [code] #include<stdio.h> int

Giang Phạm

New member
Which of the following can be the base case for the recursive implementation used to find the length of a string?
Code:
#include<stdio.h>
int get_len(char *s)
{
      int len = 0;
      while(s[len] != '\0')
        len++;
      return len;
}
int main()
{
      char *s = "";
      int len = get_len(s);
      printf("%d",len);
      return 0;
}
A. if(string[len] == 1) return 1
B. if(string[len+1] == 1) return 1
C. if(string[len] == ‘\0’) return 0
D. if(string[len] == ‘\0’) return 1
 
Answer: C
Explanation: “if(string[len] == ‘\0’) return 0” can be used as base case in the recursive implementation used to find the length of the string.
 

Tìm kiếm

Back
Top