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);
}- A. 2
- B. 1
- C. 3
- 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
- What is printed? Assume unsigned long long is 64 bits. c #include <stdio.h> int main(void) { unsigned long long p = 4000000000ULL…
- What is printed? c #include <stdio.h> int main(void) { int x = 5; int p = &x; p = p << 2 | 1; printf("%d", x); }
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t x = ~0u >> 3; int c = 0; for (; x; x &= x - 1) c++…
- What is printed? c #include <stdio.h> unsigned long long g(int n) { if (n < 2) return 0; return g(n - 1) + g(n - 2) + 1; } int main(void)…
- int c = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < i i; j++) c++; printf("%d", c); What is printed?
- int c = 0; for (int i = 1; i <= 12; i++) c += 12 / i; printf("%d", c); What is printed?