
-----------------------------------
DIIST
Wed Dec 07, 2005 5:35 pm

String Functions
-----------------------------------
Well, we just learned how to use procedures and functions, and one of our assignments was to create functions for strings. An here are some of the functions I made. I dont know how usefull they will be. I made some of them like the ctype.h functions in 'C'. And i am currently expanding them. What do you think?  /*
Author: Thuvarrakan Ruban
Date: December 6, 2005
Purpose: Usefull String Functions, I Guess!
*/


   /*
   Purpose: To check if the string is a number, retruns a zero if it is!!!
   Accepts: word:string
   Returns: int-a zero if the string is a number, an nonzero value if it isnt!
   */
    function checkNum (word:string):int
        var k:int:=0
        
        for x:1..length(word)
            if word(x)>=chr(48) and word(x)=chr(65) and word(x)=chr(97) and word(x)=chr(65) and word(x)=chr(97) and word(x)=chr(65) and word(x)=chr(97) and word(x)=chr(48) and word(x)1 and w(x-1)=" " then 
                
                else
                str+="-"
                end if
            else
                str+=w(x)
            end if
        end for
        if w(length(w))=" " then
        else
            str+="-"
        end if
        
        result str
    end sepstring
    
   /*
   Purpose: To find the number of words in a string, needed for Word function*
   Accepts:word:string
   Retruns: int-Number of words!!!
   */
    function numWord(w:string):int
        var str:string:=sepstring(w)
        var num:int:=-1
        for x:1..length(str)
            if str(x)="-" then
                num:=num+1
            end if
        end for
        result num
    end numWord
    
    /*
   Purpose: To retrun all the word in a string
   Accepts: string, word:array of string!
   Modifies: word!
   */
    procedure Word (word:string,var x:array 1..* of string) 
        var str:string:=sepstring(word)
        var num:int:=1
        var start:int:=2
        for h:1..length(str)
            if str(h)="-" and h>1 then
                x(num):=""
                for y:start..(h-1)
                    x(num)+=str(y)
                end for
                start:=h+1
                num:=num+1
            end if
        end for
    end Word


/*Example of How to use!*//*This just makes a paragraph of random word in the string!*/   
var s :string:="HI HO HELLO what are hey!! .,.,. I WHAT {}"
s:=Upper(s)%Converts to all uppercase

var xw:array 1..numWord(s) of string
Word(s,xw)%Finds and stores the words!!!

var tl:int:=0
for x:1..50
    tl:=Rand.Int(1,numWord(s))
    put xw(tl)," " ..
end for


-----------------------------------
Cervantes
Wed Dec 07, 2005 6:09 pm


-----------------------------------
Good stuff.  If you want some more ideas or code examples, I did this a while back.  
var k:string:=""
for x:1..length(word) 

or

var k : string := ""
for x : 1 .. length (word) 


2.  Return boolean values, not integers.  This makes things more understandable when using the function.  For example:

if UpperCase (my_string) then
   ...

instead of

if UpperCase (my_string) = 0 then
   ...

Further, on this topic, once you've found a lower case letter, you could return false at that instant, since you know the string is not composed entirely of lower case letters:

function UpperCase (str : string) : boolean
    for i : 1 .. length (str)
        if str (i) >= "a" and str (i) 