Selectively replace multiple instances of a character within a string: Difference between revisions
Content added Content deleted
(Add C) |
(add RPL) |
||
Line 926: | Line 926: | ||
<pre>abracadabra -> AErBcadCbFD |
<pre>abracadabra -> AErBcadCbFD |
||
caarabadrab -> cABraECdFDb</pre> |
caarabadrab -> cABraECdFDb</pre> |
||
=={{header|RPL}}== |
|||
The character "-" in the rule string means that no replacement should be made for the occurrence concerned. Any other character can be chosen by modifying the code appropriately. |
|||
Due to the use of <code>INCR</code> and <code>REPL</code> instructions, this program will only work directly on HP48 compatible RPL versions. HP28 users must have programmed their own version of these instructions. |
|||
{| class="wikitable" |
|||
! RPL code |
|||
! Comment |
|||
|- |
|||
| |
|||
≪ 0 → car rule occ |
|||
≪ 1 OVER SIZE '''FOR''' j |
|||
'''IF''' DUP j DUP SUB car == '''THEN''' |
|||
rule 'occ' INCR DUP SUB |
|||
'''IF''' DUP "-" == '''THEN''' DROP '''ELSE''' j SWAP REPL '''END''' |
|||
'''END NEXT''' |
|||
≫ ≫ ''''REPLR'''' STO |
|||
| |
|||
'''REPLR''' ''( "string" "character" "rule" -- "string" ) '' |
|||
loop for j = 1 to string length |
|||
if string[j] == character |
|||
get todo = rule[++occ] |
|||
replace if todo is different from "-" |
|||
end if end loop |
|||
return string |
|||
|} |
|||
{{in}} |
|||
<pre> |
|||
"abracadabra" |
|||
≪ "a" "AB-CD" REPLR "b" "E" REPLR "r" "-F" REPLR ≫ EVAL |
|||
</pre> |
|||
{{out}} |
|||
<pre> |
|||
1: "AErBcadCbFD" |
|||
</pre> |
|||
=={{header|sed}}== |
=={{header|sed}}== |