ProgrammingHardTCS NQT · Wipro
What is printed?
What is printed?
#include <stdio.h>
int pal(const char *s, int n) {
int l = 0, r = n - 1;
while (l < r) {
if (s[l] != s[r]) return 0;
l++; r--;
}
return 1;
}
int main(void) {
printf("%d", pal("racecar", 7));
}- A. 0
- B. 2
- C. 1
- D. 3
Correct answer: C. 1
The two pointers move inward and compare matching ends. Every pair in racecar matches, so the function returns 1.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int m[3][5]; for (int i = 0; i < 3; i++) for (int j = 0; j < 5; j++) m[i][j] = i 5…
- What is printed? c #include <stdio.h> void swap(int a, int b) { int t = a; a = b; b = t; } int main(void) { int x = 1, y = 2; int p = &x…
- What is printed? c #include <stdio.h> void rev(int a, int l, int r) { while (l < r) { int t = a[l]; a[l] = a[r]; a[r] = t; l++; r--; } }…
- What is printed? c #include <stdio.h> int main(void) { static const unsigned char t[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; unsigned x =…
- How many times does the value change between neighbours in {1, 1, 2, 2, 2, 3, 1}? Compute with this program. c #include <stdio.h> int…
- How many numbers from 1 to 100 are divisible by 4 but not by 3? Compute with this program. c #include <stdio.h> int main(void) { int c =…