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

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




PostPosted: Thu Apr 03, 2003 11:49 am   Post subject: String manipulation

This code takes a sentence and capitolizes the letters of each word, try it!

Quote:
var phrase, newPhrase : string := "" %the variables holding the unrevised and revised phrases
loop
put "Enter your phrase, Enter exit to stop: " ..
get phrase : * %get the whole phrase
exit when phrase = "exit"
exit when phrase = "Exit"
exit when phrase = "EXIT" %sentinals to exit the loop

for letter : 1 .. length (phrase)
if letter = 1 then
if phrase (letter .. letter) >= "a" and phrase (letter ..
letter) <=
"z" then
newPhrase := newPhrase + chr (ord (phrase (1 .. 1)) - 32) %capitolizes the first letter automaticly
else
newPhrase := newPhrase + phrase (1 .. 1) %if first letter isn't a small letter, it is put in where it came from
end if
elsif phrase (letter - 1 .. letter - 1) = " " then
newPhrase := newPhrase + chr (ord (phrase (letter .. letter)) -
32)
elsif phrase (letter .. letter) < "a" or phrase (letter .. letter) >
"z" then
if phrase (letter .. letter) = " " then
newPhrase := newPhrase + " "
end if
else
newPhrase := newPhrase + phrase (letter .. letter)
end if
end for
put newPhrase
end loop


im in a hurry, will explain the commands used later
Sponsor
Sponsor
Sponsor
sponsor
Asok




PostPosted: Thu Apr 03, 2003 1:41 pm   Post subject: (No subject)

it seems to bug out when you type full phrases in caps.
Vicous




PostPosted: Mon Apr 07, 2003 1:43 pm   Post subject: (No subject)

srry it took me so long to get back to this, here is the explanation

adding strings together is done with the addition command, such as...
put "O" + "K" (use variables instead of letters when needed, this also works with string variables e.g. phrase= phrase + word)

you can also find how many charectors are in a word using the length feature, e.g.

get word
put "There are ", length(word), " Charectors in your word."

this can be used in for loops to get parts of a string, explained next

substrings, that is a section of a string that you have extracted, can be aquired using the substring command ( a set of brackets after the word/variable with a number in it, or several numbers, e.g.

word(1) will get the first letter of the word in the variable word.
word(2..4) will get you the second, third and forth letter in the string word

you can find a pattern in a string using the index command. e.g.
index("Getting","t") will return a value of 3, as the third letter is the first t found (Use a variable for the word getting or the pattern to look for when necessary)

Ill leave the rest to you, but some examples of where this is useful is password programs, adding brackets to user inputed phone numbers, etc.

p.s. make fun of my spelling mistakes if you want to, I can take it Very Happy
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: