Count the Keywords
Given a line of space-separated words, count how many are Java reserved words.
Treat these as the reserved words (plus the literals true, false, null): abstract assert boolean break byte case catch char class const continue default do double else enum extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static strictfp super switch synchronized this throw throws transient try void volatile while.
Input: one line of words separated by single spaces.
Output: one line — the count of words that are reserved.
int x class for
3
- 1 <= number of words <= 50
Hint 1
line.split("\\s+") gives the words.
Hint 2
For each word, check RESERVED.contains(word) and add 1 if true.
Split the line into words and test each against the reserved-word set. Ordinary names like myVar or main (which is not reserved) do not count.