LearnFrenzy
Sign in Take a free test

C Library Functions · Predict the Output · Question 8

toupper and isdigit over a string Answer, explanation & discussion

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

What is the output of the following C program?

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

int main(void) {
    const char *s = "c23 rocks!";
    int digits = 0;
    for (int i = 0; s[i]; i++) {
        if (isdigit((unsigned char) s[i]))
            digits++;
        putchar(toupper((unsigned char) s[i]));
    }
    printf(" %d\n", digits);
    return 0;
}
  • A C23 ROCKS! 2 Correct
  • B C23 ROCKS! 0
  • C c23 rocks! 2
  • D C ROCKS! 2
Correct Answer: Option A

toupper changes only lowercase letters; digits and punctuation pass through unchanged. Two characters are digits.

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