ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
int len(const char *s) { return *s ? 1 + len(s + 1) : 0; }
int cnt(const char *s, char c) { return !*s ? 0 : (*s == c) + cnt(s + 1, c); }
int main(void) {
const char *w = "mississippi";
printf("%d %d", len(w), cnt(w, 's') * cnt(w, 'p'));
}- A. 11 8
- B. 12 8
- C. 11 12
- D. 11 6
Correct answer: A. 11 8
len counts characters up to the '\0': 11 (not 12, which would count the terminator). (*s == c) is 1 or 0, so cnt adds up matches: 4 s's and 2 p's, product 8.
More C Programming questions
- What is printed? c int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); } int main(void) { printf("%d %d", dr(9875), dr(99999)); }
- What is printed? c unsigned x = 0xF0F0; x = x - ((x >> 1) & 0x5555); x = (x & 0x3333) + ((x >> 2) & 0x3333); x = (x + (x >> 4)) & 0x0F0F…
- What is printed? c struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}}; int s = 0; for (int i = 0; i != -1; i =…
- Given int x; const int pc; int p; int const cp = &x;, which assignment is a constraint violation that the compiler must diagnose?
- What is printed? c #define S(x) #x printf("%zu %zu", strlen(S("hi")), sizeof(S(ab)));
- What is printed? c int paths(int r, int c) { return (r == 0 || c == 0) ? 1 : paths(r - 1, c) + paths(r, c - 1); } int main(void) {…