Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 String Functions
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DIIST




PostPosted: Wed Dec 07, 2005 5:35 pm   Post subject: 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?
code:
/*
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(57) then
            else
                k+=1
            end if
        end for
       
    result k
    end checkNum

   
   /*
   Purpose: To check if the string is all uppercase alphabets, retruns a zero if it is!!!
   Accepts: word:string
   Returns: int-a zero if the string is in the upperfeild of the alphabet, an nonzero value if it isnt!
   */
    function checkUAlpha (word:string):int
        var k:int:=0
       
        for x:1..length(word)
            if word(x)>=chr(65) and word(x)<=chr(90)  then
            else
                k+=1
            end if
        end for
       
        result k
    end checkUAlpha

    /*
   Purpose: To check if the string is all lowercase alphabets, retruns a zero if it is!!!
   Accepts: word:string
   Returns: int-a zero if the string is in the lower feild of the alphabet, an nonzero value if it isnt!
   */
   function checkLAlpha (word:string):int
        var k:int:=0
       
        for x:1..length(word)
            if word(x)>=chr(97) and word(x)<=chr(122)  then
            else
                k+=1
            end if
        end for
       
        result k
    end checkLAlpha


    /*
   Purpose: To check if the string is all alphabets, retruns a zero if it is!!!
   Accepts: word:string
   Returns: int-a zero if the string is of alphabets, an nonzero value if it isnt!
   */
    function checkAlpha (word:string):int
        var k:int:=0
       
        for x:1..length(word)
            if word(x)>=chr(65) and word(x)<=chr(90) or word(x)>=chr(97) and word(x)<=chr(122) then
            else
                k+=1
            end if
        end for
       
        result k
    end checkAlpha


    /*
   Purpose: To check if the string is alphanumric, retruns a zero if it is!!!
   Accepts: word:string
   Returns: int-a zero if the string is alphanumeric, an nonzero value if it isnt!
   */
    function checkAlphaN (word:string):int
        var k:int:=0
       
        for x:1..length(word)
            if word(x)>=chr(65) and word(x)<=chr(90) or word(x)>=chr(97) and word(x)<=chr(122) or word(x)>=chr(48) and word(x)<=chr(57) then
            else
                k+=1
            end if
        end for
       
        result k
    end checkAlphaN


    /*
   Purpose: Converts to string to lower case!
   Accepts: word:string
   Returns: string-all letters in lower case!
   */
   function Lower (word:string):string
        var k:string:=""
        for x:1..length(word)
            if checkUAlpha(word(x))=0 then
                k+=chr(ord(word(x))+32)   
            else
                k+=word(x)
            end if
        end for
   
        result k       
    end Lower


   /*
   Purpose: Converts to string to upper case!
   Accepts: word:string
   Returns: string-all letters in upper case!
   */
    function Upper (word:string):string
        var k:string:=""
        for x:1..length(word)
            if checkLAlpha(word(x))=0 then
                k+=chr(ord(word(x))-32)   
            else
                k+=word(x)
            end if
        end for
   
        result k       
    end Upper


   /*
   Purpose: Removes Spaces for string
   Accepts: word:string
   Returns: string-all spaces removed!
   */
    function rmspace (w:string):string
        var str:string:=""
   
        for x:1..length(w)
            if w(x)=" " then
            else
                str+=w(x)
            end if
        end for
   
        result str
    end rmspace
   
    /*
   Purpose: Required for numWord & Word functions!
   */
    function sepstring (w:string):string
        var str:string:="-"
   
        for x:1..length(w)
            if w(x)=" " then
                if  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
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Dec 07, 2005 6:09 pm   Post subject: (No subject)

Good stuff. If you want some more ideas or code examples, I did this a while back. Link!

A few comments:

1. Use whitespace. It makes things more readable. Which is nicer to look at:
code:

var k:string:=""
for x:1..length(word)

or
code:

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:
code:

if UpperCase (my_string) then
   ...

instead of
code:

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:
code:

function UpperCase (str : string) : boolean
    for i : 1 .. length (str)
        if str (i) >= "a" and str (i) <= "z" then
            result false
        end if
    end for
    result true
end UpperCase


3. I realize you haven't got this far, but this hunk of code just begs for a home. What's it's home? The String module. Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: