Grad
Grad

Reputation: 21

Find nth occurance of character in Rexx

I want to find a nth occurance of character in string in Rexx. Is there any built in function to do this? If no, how can I do this?

Upvotes: 2

Views: 1361

Answers (4)

psk
psk

Reputation: 1

public class character_occurance {

    public static void main(String[] args) {
        
        
        String str1 = "bcdabghbghjbjkib";
        int count = 0;
        for (int i = 0; i <str1.length(); i++) {
            if (str1.charAt(i) == 'j') {
                count=count+1;
            }

        }System.out.println(count);

    }

}

Upvotes: -1

apo
apo

Reputation: 116

Parse is nice. Usually keeps things short. Here's one way of using it in context of this Q:

/* REXX: PosOfNth: Position of nth character(string) in haystack */            
                                                                               
/* tests */                                                                    
s='Foo Bar Baz'; c='F'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Foo Bar Baz'; c='a'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Foo Bar Baz'; c='a'; n=2; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Foo Bar Baz'; c='z'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Foo Bar Baz'; c='z'; n=2; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Lorem Ipsum'; c='m'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Lorem Ipsum'; c='z'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)             
s='Lorem Ipsum'; c='Ips'; n=1; Say n'-th 'c' in 's"="PosOfNth(s,c,n)           
                                                                               
/* "visual" test */                                                            
Say                                                                            
s='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod...' 
c='a'; n=2                                                                     
Say s; Say Left('',PosOfNth(s,c,n)-1,' ')||'^ - 'n'-th 'c' is here'            
                                                                               
Exit                                                                           
                                                                               
/* ===== PROCS ===== */                                                        
                                                                               
PosOfNth: Procedure ;                                                          
Parse Arg haystack, needle, n                                                  
posN = 0                                                                       
Do n                                                                           
  oHaystack = haystack                                                         
  Parse Value haystack With head (needle) haystack                             
  If haystack='' & oHaystack<>head||needle Then Do                             
    posN = 0                                                                   
    Leave                                                                      
  End; Else posN = posN + Length(head) + 1                                     
End     
posN = posN + (Length(needle)-1) * (n-1)                                                                       
Return posN 

EDIT As @phunsoft pointed out, there was an issue with multi-char needle (search string). I added line posN = posN + (Length(needle)-1) * (n-1) to adjust for it.

Upvotes: 2

phunsoft
phunsoft

Reputation: 2745

Here is a sample implementation of a function to return the position of the nth occurrence of a needle within a haystack. The function is called NthPositionOfNeedle.

/*- REXX --------------------------------------------------------------------------
   
  This function returns the position of the nth occurrence of a given character
  in a given string. A negative return value indicates a parameter error, or
  the fact that the nth occurrence was not found.
  
  Parameters:
  
    Haystack    A non-empty string of characters to be search.
    Needle      A single character to be searched for in the haystack.
    Count       A whole number indicating the nth occurrence to be found.
    
  Return values:
     n          where n > 0 indicates the position of the nth occurrence 
                of the needle within the haystack.
                 
    -1          The haystack is an empty string.
    -2          The needle is not a single character.
    -3          The count is not a whole number > 0.
    -4          The needle was not found at all within the haystack.
    -5          The nth occurrence was not found, but the needle was
                found at least once.                

---------------------------------------------------------------------------------*/

    
NthPositionOfNeedle: procedure

Haystack = arg(1)
Needle   = arg(2)
Count    = arg(3)

/* The haystack must not be an empty string. Return -1 if it is */
if length( Haystack ) = 0 
then return -1

/* The Needle must be a single character. Return -2 if it is not */
if length( Needle ) <> 1
then return -2

/* The count must be a whole number > 0. Return -3 if it is not */
if datatype( Count, "W" ) = 0 | Count < 1
then return -3

/* Does needle exist at least once in the haystack? Return -4 if not */
if pos( Needle, Haystack ) = 0
then return -4

CurrCnt = 0
Start   = 1

do while ( CurrCnt < Count  &  Start <= length( Haystack ) )
    NeedlePos = pos( Needle, Haystack, Start )
    
    /* No more occurrences? */
    if NeedlePos = 0
    then leave
    
    CurrCnt   = CurrCnt + 1
    Start     = NeedlePos + 1
        
    end
    
/* Return the position of the nth occurrence, if so many exist, else return zero */ 
if CurrCnt = Count
then return NeedlePos
else return -5

This is the code to test above funtion

/*- REXX --------------------------------------------------------------------------

  Code to test the function "NthPositionOfNeedle".
  
---------------------------------------------------------------------------------*/

say "1st occurrence of 't' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 't', 1 )
    /*                    ....+....1....+....2....+....3....+....4....      */

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 1 )

say "2nd occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 2 )

say "3rd occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 3 )

say "6th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 6 )

say "7th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 7 )

say "0th occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 0 )

say "2nd occurrence of 'x' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'x', 2 )

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( '', 'i', 1 )

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', '', 1 )

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 0 )

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', "a" )

say "1st occurrence of 'i' in 'this is a string with multiple characters i.' is" ,
    NthPositionOfNeedle( 'this is a string with multiple characters i.', 'i', 1.1 )



exit

Upvotes: 3

Steve Ives
Steve Ives

Reputation: 8134

Use the POS function n number of times.

E.g to find the 3rd 'a' in a string, use pos("a",string,currentPos) 3 times with the initial start point being 1 and the subsequent start points being the result of the previous pos.

/* REXX */                                                            
                                                                      
string   = "The quick brown fox jumps over the lazy dog"                                                                               
srchchar = "e"                                                        
count    = 2                                                          
                                                                      
say "The position of the '"count"' occurrence of '"srchChar"'"||,     
       " in '"string"' is at position : "                             
say findChar(srchChar, string, count)                                 
                                                                      
exit                                                                  
                                                                      
findChar: procedure                                                   
 /* Find the nth (count) position of the string needle in haystack */ 
 arg needle, haystack, count                                          
                                                                      
 currentPos = 0                                                       
                                                                      
 do count                                                             
    currentPos = currentPos + 1                                       
    currentPos = pos(needle, haystack, currentPos)                    
    end                                                               
                                                                      
 return currentPos     

If you run this, you get :

The position of the '2' occurrence of 'e' in 'The quick brown fox jumps over the lazy dog' is at position :
29                                                                                                         
***    

This will accept a string as the search argument, not just a single character. If you wanted to enforce the search argument being a single character you could, but I can't see any value in doing so as it adds code and reduces functionality.

E.g.

string   = "The quick brown fox jumps over the lazy dog"                                                                          
srchchar = "the"                                                   
count    = 2                                                       
                                                                   
say "The position of the '"count"' occurrence of '"srchChar"'"||,  
       " in '"string"' is at position : "                          
say findChar(srchChar, string, count)  
                        

returns:

The position of the '2' occurrence of 'the' in 'The quick brown fox jumps over the lazy dog' is at position : 
32                                                                                                            
***         

EDIT - This is my old (incorrect as I mis-read the quest) answer, that counts the number of times the search character appears in the string.

No built-in function that I know of, but you could write a subroutine to step through the search string character-by-character, comparing the current character with the one you are looking for:

/* REXX */                                                        
                                                                  
string = "The quick brown fox jumps over the lazy dog"            
srchchar = "q"                                                    
                                                                  
say "There is/are "count(srchchar, string)" occurrances of "||,   
       "'"srchchar"' in '"string"'"                               
                                                                  
exit                                                              
                                                                  
count: procedure                                                  
 arg needle, haystack                                             
 count = 0                                                        
 do i = 1 to length(haystack)                                     
    currentChar = substr(haystack,i,1)                            
    if currentChar = needle then count = count + 1                
    end                                                           
                                                                  
 return count 

I'm sure there are other ways too.

Upvotes: 4

Related Questions