ProgrammingModerateTCS NQT · Wipro

How many positions differ between the two strings? Compute with this program.

How many positions differ between the two strings? Compute with this program.
#include <stdio.h>
int main(void) {
    const char *p = "abcde", *q = "abXdY";
    int diff = 0;
    while (*p && *q) {
        if (*p != *q) diff++;
        p++; q++;
    }
    printf("%d", diff);
}
  1. A. 2
  2. B. 1
  3. C. 3
  4. D. 4

Correct answer: A. 2

The strings differ at index 2 (c versus X) and index 4 (e versus Y), so the loop counts 2. The loop stops at the shorter string's end.

More C Programming questions

Exams that ask this topic