ProgrammingVery hard
int pal(const char *s) { int i = 0, j = (int)strlen(s) - 1; while (i < j) if (s[i++] != s[j--]) return 0; return 1; } How many of "a", "racecar", "abca" and "ab" return 1?
- A. 1
- B. 2
- C. 3
- D. 4
Correct answer: B. 2
'a' and 'racecar' are palindromes. 'abca' passes the first pair (a, a) but fails on (b, c), and 'ab' fails at once.
More C Programming questions
- char s[] = "xyz"; for (int i = 0; s[i]; i++) s[i] = (s[i] - 'a' + 3) % 26 + 'a'; printf("%s", s); What is printed?
- char d[8] = "#######"; strncpy(d, "ab", 5); int z = 0; for (int i = 0; i < 8; i++) if (d[i] == 0) z++; printf("%d", z); What is printed?
- int a = 0, b = 0; char op = 0; int n = sscanf("12 + x", "%d %c %d", &a, &op, &b); printf("%d %c", n, op); What is printed?
- Which loop is safe for every value a plain char can hold, including bytes above 127 that may be negative?
- What is the smallest array size that holds the string "Ravi" with strcpy and no undefined behaviour?
- char s[] = "x41x420x43"; printf("%zu", strlen(s)); What is printed?