How to access single character from a string in COBOL? -


how access particular character in string in cobol?

for example if string "work" have access character 'w' in string , store in character. need matching character, , not position of character.

for example in c, following extract i'th character in string,

    char data[5] = "work";                                                                  char temp;                                                                             temp = data[3];                                                                     

temp have value 'k'

now need same in cobol.

the first thing understand array indices 0 based in c , 1 based in cobol.

next cobol , c have differnt ways of representing character strings. in c string stored array of characters, end of string typically represented using binary 0 (null \0). cobol has no such convention. strings stored in named data items of specified length. these items typically declared under working-storage , have picture clause associated them of type 'x' (there several other possibilities picture clauses 'x' represents type of character). example:

01    my-variable   pic x(20). 

the working storage variable called my-variable declared 20 characters long. may assigned value in procedure division follows.

move 'work' my-variable 

you can access various characters of string (or substrings) using technique known reference modification:

display my-variable(3:1) 

will display third character of my-variable (1 based indexing), 'r'. first number in parenthesis above (3) indicates offset beginning of variable, second number number of characters starting position (1).

there other methods of doing this, such redefines, my-variable redefined array of 20 1 character cells. outdated mechanism encourage using reference modification access parts of character strings.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -