View previous topic :: View next topic |
Author |
Message |
amit4u79 Beginner
Joined: 24 Oct 2005 Posts: 109 Topics: 36 Location: India
|
Posted: Tue Nov 08, 2005 8:53 pm Post subject: Search all the occurences of a character in a word in REXX ! |
|
|
Hi techfundoos..
I am having to code a REXX which would find the position of all occurences of a particular character in a string.
For example suppose the string is "ABXCDXDDDXEEEXGGGX", then I need to find out the position of X in the string. The output should give me something like "X is at positions 3,6,10,14,18.
I cannot use POS builtin func as that would give me only first occuring position number of X. Also, I can manipulate POS func, but I was looking if there was some straightforward way to do this.
Thanks, kindly help.
Amit Joshi
Singapore. |
|
Back to top |
|
 |
Dibakar Advanced

Joined: 02 Dec 2002 Posts: 700 Topics: 63 Location: USA
|
Posted: Wed Nov 09, 2005 5:08 am Post subject: |
|
|
Don't know if it is simple or not but here is an untested approach -
Code: | my_string = "ABXCDXDDDXEEEXGGGX"
my_char = "X"
my_message = my_char||" is at positions"
if index(my_string,my_char) = 0 then
my_message = my_message||' 0'
else
do m = 1 to length(my_string)
if substr(my_string,m,1) = my_char then
my_message = my_message||' 'm
end |
This would give an output like "X is at positions 3 6 10 14 18"
Regards,
Diba. |
|
Back to top |
|
 |
danm Intermediate
Joined: 29 Jun 2004 Posts: 170 Topics: 73
|
Posted: Wed Nov 09, 2005 8:58 am Post subject: |
|
|
Code: |
/* REXX */
String = 'ABXCDXDDDXEEEXGGGX'
match_char = 'X'
positions = ''
Start_pos = 1
Do until idx = 0
idx = pos(match_char,String,Start_pos)
If idx <> 0 then positions = positions idx
Start_pos = idx + 1
End /* Do until idx = 0 */
say 'character string =' String
If positions = '' then say 'Character' match_char 'not found'
Else say 'Character' match_char 'found in :' space(positions,1,',')
Exit
|
|
|
Back to top |
|
 |
amit4u79 Beginner
Joined: 24 Oct 2005 Posts: 109 Topics: 36 Location: India
|
Posted: Thu Nov 10, 2005 1:20 am Post subject: |
|
|
Hey thanks a lot for giving me the code for my requirement. It really helped save a lot of time and effort on my part.
Thanks again,
Amit Joshi
Singapore _________________ I did not fail; I have found 10,000 ways that would not work - Albert Einstein. |
|
Back to top |
|
 |
|
|