LearnFrenzy
Sign in Take a free test

C Const · Predict the Output · Question 7

Counting vowels with const strings and strchr() Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>
#include <string.h>

int main(void) {
    static const char vowels[] = "aeiou";
    const char *word = "constant";
    int n = 0;
    for (const char *p = word; *p; p++)
        if (strchr(vowels, *p))
            n++;
    printf("%d\n", n);
    return 0;
}
  • A 2 Correct
  • B 3
  • C 1
  • D 8
Correct Answer: Option A

"constant" contains o and a. strchr() takes a const char *, so const data passes straight in. (Careful: strchr(s, '\0') finds the terminator, but the loop never passes '\0'.)

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links