# Programming Example 2: Character Testing

### Problem Statement

This is an extension of an example discussed in SELECT CASE statement. This program reads in a character and determines if it is a vowel, a consonant, a digit, one of the four arithmetic operators, a space, or something else (i.e., %, \$, @, etc).

### Solution

```! ------------------------------------------------------------
! This program reads in a single character and determines if
! it is a vowel, a consonant, a digit, one of the four
! arithmetic operators (+, -, * and /), a space, or something
! else.  You can do it with IF-THEN-ELSE-END IF statement; but
! SELECT CASE statement provides a cleaner solution.
!
! For character input, you could use the quote characters like
!         'G'
! Or, just type the character.  In this case, the first
! character you type will be read.
! ------------------------------------------------------------

PROGRAM  CharacterTesting
IMPLICIT  NONE

CHARACTER(LEN=1) :: Input

SELECT CASE (Input)
CASE ('A' : 'Z', 'a' : 'z')       ! rule out letters
WRITE(*,*)  'A letter is found : "', Input, '"'
SELECT CASE (Input)            ! a vowel ?
CASE ('A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o','u')
WRITE(*,*)  'It is a vowel'
CASE DEFAULT                ! it must be a consonant
WRITE(*,*)  'It is a consonant'
END SELECT
CASE ('0' : '9')                  ! a digit
WRITE(*,*)  'A digit is found : "', Input, '"'
CASE ('+', '-', '*', '/')         ! an operator
WRITE(*,*)  'An operator is found : "', Input, '"'
CASE (' ')                        ! space
WRITE(*,*)  'A space is found : "', Input, '"'
CASE DEFAULT                      ! something else
WRITE(*,*)  'Something else found : "', Input, '"'
END SELECT

END PROGRAM  CharacterTesting
```

### Program Input and Output

How do you input a character to your program? The easiest way is using something like 'Y', "g" and '-'. That is, use single or double quotes.
• If the input character is s, the program should report it is a consonant.
```A letter is found : "s"
It is a consonant
```
• If the input character is U, the program should report it is a vowel.
```A letter is found : "U"
It is a vowel
```
• If the input character is 8, the program should report it is a digit.
```A digit is found : "8"
```
• If the input character is +, the program should report it is an operator.
```An operator is found : "+"
```
• If the input character is a space, the program should report it is a space.
```A space is found : " "
```
• If the input character is none of the above, say &, the program should report it is something else.
```Something else found : "&"
```

### Discussion

To be a vowel or a consonant, the input character must be a lower or upper case letter. So, the first CASE of the outer SELECT CASE singles out all letters. Within the first CASE, another SELECT CASE is used to separate vowels from consonants. Since a letter is either a vowel or a consonant, we use the shorter list (i.e., vowels) in the CASE label and let CASE DEFAULT deal with the longer list of consonants.