Page 1 of 1

the position of the last character inside a string

PostPosted: Fri Aug 06, 2010 11:13 pm
by vegafacundodaniel
Hello,
I'd want to know, for a string of 10 characters, what is the position of the last character wrote inside the string (or the first position after the last character wrote inside the string)

Example:

TOTO --------- String X(10).

MOVE 'ABCDEF ' to TOTO.

TOTO = 'ABCDEF '

the position will be 6. (or 7)

Thanks in advance

Re: the position of the last character inside a string

PostPosted: Fri Aug 06, 2010 11:25 pm
by NicC
you posted the same query a few days ago and got the answer. Suggest you re-read it - it is only about 10 topics down the screen.

Re: the position of the last character inside a string

PostPosted: Fri Aug 06, 2010 11:27 pm
by Robert Sample
First, be aware that COBOL -- unlike C or similar languages -- does not have the concept of "string". A COBOL variable defined in WORKING-STORAGE as PIC X(10) will always contain 10 bytes -- never more, never less.

Second, you did not mention which COBOL compiler you are using -- VS COBOL II, for example, does not have some of the functionality of Enterprise COBOL for MVS & VM and the solution varies slightly by compiler.

Assuming you're using a reasonably recent COBOL compiler (always a dangerous assumption), you can either (1) use reference modification from byte 10 to 1 looking for the first non-blank character, or (2) use FUNCTION REVERSE on the variable and INSPECT tallying leading spaces, then subtract the tally count from 10.

A method that would work for all versions of COBOL would be to redefine the variable as a 10-byte array and look at each byte from 10 to 1 to find the first non-blank character.

Re: the position of the last character inside a string

PostPosted: Wed Aug 11, 2010 12:24 am
by vegafacundodaniel
You are right Nic. I posted the same query but I didn't want that.

After I gave my solution using only the INSPECT function, I got en error because my variable X(20) may have spaces between de characters.

For example :
TOTO = 'ABCDE HIJ LMN '

I didn't explain that before.... Sorry

Now, Instead of using the INSPECT function, I am using INSPECT FUNCTION REVERSE. It works fine !!!

Regards !

Re: the position of the last character inside a string

PostPosted: Wed Aug 11, 2010 1:38 am
by NicC
Well, you shaould have just continued the same thread. Also, if you had fully read my post I said 'reversed string'. Anyway, you got there in the end!