Master Mainframe Technologies - COBOL, JCL, DB2, VSAM, CICS & More
ABEND Codes SQLCODEs File Status Interview Prep Contact
← Back to String Handling
String Handling | Intermediate | COBOL

Count Vowels

Problem Description

Count the number of vowels in a given string.

Expected Output

Input: HELLO WORLD -> Output: VOWELS = 3

Hints

Use INSPECT TALLYING for each vowel.

Solution

IDENTIFICATION DIVISION. PROGRAM-ID. VOWELCNT. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-INPUT PIC X(100). 01 WS-COUNT PIC 9(3) VALUE 0. PROCEDURE DIVISION. DISPLAY "ENTER A STRING: ". ACCEPT WS-INPUT. INSPECT WS-INPUT TALLYING WS-COUNT FOR ALL "A" "E" "I" "O" "U" "a" "e" "i" "o" "u". DISPLAY "VOWEL COUNT = " WS-COUNT. STOP RUN.

Explanation:

INSPECT TALLYING counts occurrences. FOR ALL counts each character.