How to resolve the algorithm Determine if a string is numeric step by step in the COBOL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Determine if a string is numeric step by step in the COBOL programming language
Table of Contents
Problem Statement
Create a boolean function which takes in a string and tells whether it is a numeric string (floating point and negative numbers included) in the syntax the language uses for numeric literals or numbers converted from strings.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Determine if a string is numeric step by step in the COBOL programming language
Source code in the cobol programming language
program-id. is-numeric.
procedure division.
display function test-numval-f("abc") end-display
display function test-numval-f("-123.01E+3") end-display
if function test-numval-f("+123.123") equal zero then
display "is numeric" end-display
else
display "failed numval-f test" end-display
end-if
goback.
IDENTIFICATION DIVISION.
PROGRAM-ID. Is-Numeric.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Numeric-Chars PIC X(10) VALUE "0123456789".
01 Success CONSTANT 0.
01 Failure CONSTANT 128.
LOCAL-STORAGE SECTION.
01 I PIC 99.
01 Num-Decimal-Points PIC 99.
01 Num-Valid-Chars PIC 99.
LINKAGE SECTION.
01 Str PIC X(30).
PROCEDURE DIVISION USING Str.
IF Str = SPACES
MOVE Failure TO Return-Code
GOBACK
END-IF
MOVE FUNCTION TRIM(Str) TO Str
INSPECT Str TALLYING Num-Decimal-Points FOR ALL "."
IF Num-Decimal-Points > 1
MOVE Failure TO Return-Code
GOBACK
ELSE
ADD Num-Decimal-Points TO Num-Valid-Chars
END-IF
IF Str (1:1) = "-" OR "+"
ADD 1 TO Num-Valid-Chars
END-IF
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10
INSPECT Str TALLYING Num-Valid-Chars
FOR ALL Numeric-Chars (I:1) BEFORE SPACE
END-PERFORM
INSPECT Str TALLYING Num-Valid-Chars FOR TRAILING SPACES
IF Num-Valid-Chars = FUNCTION LENGTH(Str)
MOVE Success TO Return-Code
ELSE
MOVE Failure TO Return-Code
END-IF
GOBACK
.
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the Pascal programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Haskell programming language
You may also check:How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the C fast fixed-point programming language