bugmagnet
bugmagnet

Reputation: 7769

Counting uppercase and lowercase. A better way to specify the alphabets?

Given this working version

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 UC-COUNTER PIC 99.
    01 LC-COUNTER PIC 99.
PROCEDURE DIVISION.
    INSPECT "My dog has fleas"
     TALLYING UC-COUNTER FOR ALL 'A','B','C','D','E','F',
        'G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
        'U','V','W','X','Y','Z' 
        LC-COUNTER FOR ALL 'a','b','c','d','e','f',
        'g','h','i','j','k','l','m','n','o','p','q','r','s','t',
        'u','v','w','x','y','z'.
    DISPLAY UC-COUNTER " UPPER CASE CHARACTERS".
    DISPLAY LC-COUNTER " LOWER CASE CHARACTERS".
    
    GOBACK.

Is there a better way of expressing the alphabets? That is, can we have a SPECIAL-NAMES or something like that so that the instruction can be INSPECT TALLYING FOR ALL LATIN-UPPERCASE-LETTERS-ENGLISH or something similar.

LATER

Things like this don't appear to work (and given the GnuCOBOL docs, probably won't)

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
  CLASS UPPER-CASE-ENGLISH-LETTERS IS
   'A','B','C','D','E','F','G','H','I','J','K','L','M',
   'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'.
DATA DIVISION.
WORKING-STORAGE SECTION.
    01 UC-COUNTER PIC 99.
PROCEDURE DIVISION.
    INSPECT "My dog has fleas"
     TALLYING UC-COUNTER FOR ALL UPPER-CASE-ENGLISH-LETTERS.
    DISPLAY UC-COUNTER " UPPER CASE CHARACTERS".
    STOP RUN.

Upvotes: 1

Views: 391

Answers (2)

Rick Smith
Rick Smith

Reputation: 4407

Is there a better way of expressing the alphabets?

COBOL has the class conditions ALPHABETIC-UPPER and ALPHABETIC-LOWER. Each of these includes space as part of the class condition. Unfortunately, for what you are requesting, class conditions cannot be used in an INSPECT statement.

It is necessary to simulate the INSPECT statement by using a loop to inspect each character to determine if it meets the class conditions while excluding spaces.

Code:

   data division.
   working-storage section.
   01 uc-counter pic 99.
   01 lc-counter pic 99.
   01 str pic x(50).
   01 n comp pic 9(4).
   procedure division.
       initialize uc-counter lc-counter
       string "My dog has fleas" low-value delimited size
           into str
       perform varying n from 1 by 1
       until str(n:1) = low-value
           evaluate true
           when str(n:1) = space
               continue             *> ignore space
           when str(n:1) is alphabetic-upper
               add 1 to uc-counter  *> count upper-case letter
           when str(n:1) is alphabetic-lower
               add 1 to lc-counter  *> count lower-case letter
           when other
               continue
           end-evaluate
       end-perform
       display uc-counter " upper case characters"
       display lc-counter " lower case characters"
       goback
       .

Output:

01 upper case characters
12 lower case characters

Upvotes: 3

hkBst
hkBst

Reputation: 3360

Depending on what character set you are using, if the characters are contiguous, then you could suffice with a few comparisons.

Upvotes: 0

Related Questions