ProgrammingHardTCS NQT · Wipro
How many occurrences of the two-letter string "ab" are in "abcabab"? Count them with this program.
How many occurrences of the two-letter string "ab" are in "abcabab"? Count them with this program.
#include <stdio.h>
int main(void) {
const char *s = "abcabab";
int c = 0;
for (int i = 0; s[i] && s[i + 1]; i++)
if (s[i] == 'a' && s[i + 1] == 'b') c++;
printf("%d", c);
}- A. 2
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
The matches start at positions 0, 3 and 5 of abcabab, so there are three.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int c = 0; switch ('b') { case 'a': c++; case 'b': c += 10; case 'c': c += 100…
- How many nodes are at depth 2 in a perfect binary tree of depth 3 (the root is depth 0)? The program counts them recursively. c #include…
- What is printed? Each struct has an int a and an int b. c #include <stdio.h> #include <stdlib.h> struct P { int a, b; }; int cmp(const…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 11, y = x & (x + 1); printf("%u", y); }
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int (p)[3] = 0; printf("%zu", sizeof p); }
- What is printed? Assume the array has space for 5 characters. c #include <stdio.h> int main(void) { char s[5] = "abcd"; printf("%zu"…