/*
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
|