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);
}
  1. A. 3
  2. B. 1
  3. C. 4
  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

Exams that ask this topic