Tips & Trix
On the case: Converting Lower Case Characters to Upper Case and vice versa
Home
PUBz
Grafx Pax
Tips & Trix
Neo-Linx
Neo-Talk
Neo-Filez
Search NeoDezign
 

 You might find a situation where you need to convert user input text from lower case to upper case, (or upper to lower case), to maintain consistancy in your program. In the NeoBook\Samples folder there is a jewel of a script in the HangMan example game made by NeoSoft that does this quite effectively. This slick case convert routine uses some fancy string manipulation in a loop to get the job done. 

Let's pick it apart to learn how it works... 


... 'Prime the pump' giving X a value of 1
SetVar "[X]" "1"

... Get the length of the text string
StrLen "[Word]" "[WordLen]"

... Initiate a loop until X equals the length of the text string
While "[X]" "<=" "[WordLen]"

... Pick apart the string one character at a time. 
... Store this charater in variable C
 SubStr "[Word]" "[X]" "1" "[C]"

... Search this Alphabet string for the match of C and store 
... the resulting postion number in the variable CP 
 SearchStr "[C]" "abcdefghijklmnopqrstuvwxyz" "[CP]"

... Only act if the character C was found in the Alphabet string 
... At this point CP has a numerical value. This value is the position 
... of the C character in the Alphbet string
  If "[CP]" ">" "0"

... Delete the character at position X
    StrDel "[Word]" "[X]" "1" "[Word]"

... Pick out the uppercase character from the same position 
... it was found in the lowercase Alphabet string
... Store this new character in the variable C
    SubStr "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "[CP]" "1" "[C]"

... Place this uppercase character into the same position 
... where the lowercase character was deleted from
    StrIns "[C]" "[Word]" "[X]" "[Word]"

... all done with this character - get outta here
  EndIf

... Add 1 to the X loop to move on to the next character in the text string
  Math "[X]+1" "0" "[X]"

... Visual indicator
  SetVar "[Progress]" "[Progress]•"

... The bottom of the loop. If X is less than or equal to 
... the length of the text string then we will go back to 
... the While line to repeat the sequence.
EndWhile

 

Download an example PUB