ProgrammingModerateTCS NQT · Wipro
How many times does the letter g occur in "programming"? The program counts it with a pointer walk.
How many times does the letter g occur in "programming"? The program counts it with a pointer walk.
#include <stdio.h>
int main(void) {
const char *s = "programming";
int c = 0;
for (; *s; s++)
if (*s == 'g') c++;
printf("%d", c);
}- A. 3
- B. 1
- C. 4
- D. 2
Correct answer: D. 2
The letter g appears at positions 3 and 10 of programming, so the count is 2.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int x = 10; printf("%d %d", x & -x, x & (x - 1)); }
- What is printed? c #include <stdio.h> int main(void) { int v[] = {3, 9, 4}; int p = v; int s = 0; while (p < v + 3) s += p++ 2…
- What is printed? c #include <stdio.h> int main(void) { printf("%c", 'Z' - 'A' + 'a'); }
- What is printed? c #include <stdio.h> #include <stdlib.h> int nd(int n) { int c = 0; for (int d = 1; d <= n; d++) if (n % d == 0) c++…
- How many binary strings of length 4 have exactly two 1s? The program counts them recursively. What is printed? c #include <stdio.h> int…
- What is printed? c #include <stdio.h> int v[] = {1, 2, 3}; int cnt(int i, int s) { if (i == 3) return s % 2 == 0; return cnt(i + 1, s) +…