Created
July 14, 2014 03:41
-
-
Save coplate/fdc67cf958058f4dbe10 to your computer and use it in GitHub Desktop.
/r/dailyprogrammer: [6/27/2014] Challenge #168 [Easy] String Index - COBOL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IDENTIFICATION DIVISION. | |
PROGRAM-ID. StringIndex. | |
DATA DIVISION. | |
WORKING-STORAGE SECTION. | |
01 tmp PIC 99. | |
01 srcString PIC X(1000) VALUE "...You...!!!@!3124131212 Hello have this is a --- string Solved !!...? to test @ | |
#!#@#@%$**#$@ Congratz this!!!!!!!!!!!!!!!!one ---Problem | |
". | |
01 dstString PIC X(1000). | |
01 varWordIndex PIC S999. | |
PROCEDURE DIVISION. | |
*>Words is defined as [a-zA-Z0-9]+ so at least one of these and many more in a row defines a word. | |
*>Any other character is just a buffer between words." | |
*>Index can be any integer (this oddly enough includes negative value). | |
*>If the index into the string does not make sense because the word does not exist then return an empty string. | |
INSPECT srcString CONVERTING X'0102030405060708090A0B0C0D0E0F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'101112131415161718191A1B1C1D1E1F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'202122232425262728292A2B2C2D2F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'3A3B3C3D3E3F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'40' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'5B5C5D5E5F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'60' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
INSPECT srcString CONVERTING X'7B7C7D7E7F' TO X'2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E2E' | |
DISPLAY srcString | |
MOVE 12 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE -1 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 1 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE -100 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 4 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 1000 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 9 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE -1000 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 16 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 13 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 17 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
MOVE 15 to varWordIndex | |
CALL "WordIndex" using BY REFERENCE varWordIndex,srcString,dstString. | |
STOP RUN | |
. | |
IDENTIFICATION DIVISION. | |
PROGRAM-ID. WordIndex IS INITIAL. | |
DATA DIVISION. | |
WORKING-STORAGE SECTION. | |
01 i PIC 9(4). | |
01 j PIC 9(4). | |
01 tmpString PIC X(1000). | |
01 strPointer PIC 9(4). | |
LINKAGE SECTION. | |
01 paramWordIndex PIC S999. | |
01 paramSrcString PIC X(1000). | |
01 paramDstString PIC X(1000). | |
PROCEDURE DIVISION USING paramWordIndex, paramSrcString,paramDstString. | |
Begin. | |
IF paramWordIndex > 0 THEN | |
ADD 1 TO paramWordIndex | |
MOVE SPACE TO paramDstString | |
MOVE 1 to strPointer | |
PERFORM VARYING i FROM 1 BY 1 UNTIL i > paramWordIndex | |
MOVE SPACE TO tmpString | |
UNSTRING paramSrcString DELIMITED BY ALL "." | |
INTO tmpString | |
WITH POINTER strPointer | |
END-UNSTRING | |
END-PERFORM | |
IF tmpString NOT = SPACES THEN | |
DISPLAY paramWordIndex ">" tmpString | |
END-IF | |
END-IF | |
. | |
END PROGRAM WordIndex. | |
END PROGRAM StringIndex. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment