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

Username:   Password: 
 RegisterRegister   
 [Tutorial]Index
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AsianSensation




PostPosted: Mon Mar 08, 2004 10:43 pm   Post subject: [Tutorial]Index

the index command

I was looking around the tutorial section, and bearing in mind many people are having problems with string manipulation, I searched the forum for a tutorial on index, one of the most important function a Turing user could come across. So seeing the absence of the tutorial, I'll write one up.

index command: What is it?
index returns the position of the first occurrence of a string in another string. Straight from the help file:
code:
var word : string := "willing"
put index ( word, "ill" )


this will output 2, because "ill" occurs at the 2nd letter of the string "willing".

So why is it useful?

the ability to search for a string inside another string is very useful. combining this with some ingenuity on the user's part, a hard question could most likely be solved. index will also reduce the amount of if statements a person has to code if he/she is doing string manipulation.

ex. Get a word from the user, keep track of how many A's, how many B's, etc. Separate uppercase from lower case. So "A" is different from "a".

without ingenuity, you would do this:

code:
for rep : 1 .. length (word)
    if word (rep) = "A"
        ...
    elsif word (rep) = "B"
        ...
    elsif ...
    .
    .
    .
    end if
end for


with some thinking, but no index:
code:
var alpha := "ABCD...XYZabcd...xyz"

for rep : 1 .. length (word)
    for rep2 : 1 .. length (alpha)
        if word (rep) = alpha (rep2)
        end if
    end for
end for


now take that idea and index it:
code:
var alpha := "ABCD...XYZabcd...xyz"

for rep : 1 .. length (word)
    if index (alpha, word (rep)) not= 0 then
    end if
end for


seems much easier to follow and to code, right?

Things to notice:

however wonderful index seems to be, it does have it's disadvantages. Index finds the position by comparing each one of letters from the pattern to the designated word. This could be a issue in terms of speed. If you are comparing millions of things at once (well, first of all, you wouldn't be using turing), it could slow down your program by alot. Secondly, index searches the word from the beginning to the end. So make sure you restrict the parameter in which index searches. Meaning, if you know the word you want is somewhere in the middle, don't start the search from the beginning. Many times you have to cut down the string, and then index from the beginning, it's the same idea.

well, that's for index. Know this though, string manipulation comes with practice, and problem solving is a big part of this. So don't assume knowing all about index will make sure you will be able to solve a question. Index is merely a tool, not the solution.
Sponsor
Sponsor
Sponsor
sponsor
ryanlisim




PostPosted: Fri Nov 23, 2007 3:26 am   Post subject: RE:[Tutorial]Index

lets say i want to use the following:

var alpha := "ABCD...XYZabcd...xyz"

for rep : 1 .. length (word)
for rep2 : 1 .. length (alpha)
if word (rep) = alpha (rep2)
end if
end for
end for

but i need to do it for the whole alphabet. is there some way to loop it and do all the letters from the alphabet?
Clayton




PostPosted: Sat Nov 24, 2007 5:33 pm   Post subject: RE:[Tutorial]Index

Use ASCII values of characters.
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: