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

Username:   Password: 
 RegisterRegister   
 questions about index function
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
xtreemboarder




PostPosted: Sun Jun 11, 2006 5:51 pm   Post subject: questions about index function

how can i make it start searching at a defined position in a string?

also, how can i make it search for spaces (" ")?
 
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Jun 11, 2006 6:18 pm   Post subject: (No subject)

xtreemboarder wrote:

how can i make it start searching at a defined position in a string?

A good question. Try this:
code:

var the_string := "Hello world"
var short_string := the_string (3 .. *)    % short_string = "llo world"
put index (short_string, "w")                   % outputs 5

But that doesn't quite return what we want, does it? No, because the 'w' is the 7th character of 'the_string'.
code:

var offset := 3
var the_string := "Hello world"
var short_string := the_string (offset .. *)    % short_string = "llo world"
put index (short_string, "w") + offset - 1      % outputs 5


We can compress that into one line (not including the declaration of 'offset'):
code:

put index (the_string (offset .. *), "w") + offset - 1


xtreemboarder wrote:

also, how can i make it search for spaces (" ")?

Just like that.
code:

put index ("Hello World", " ")      % outputs 6


All code here untested.
 
xtreemboarder




PostPosted: Sun Jun 11, 2006 8:25 pm   Post subject: (No subject)

Cervantes wrote:
xtreemboarder wrote:

how can i make it start searching at a defined position in a string?

A good question. Try this:
code:

var the_string := "Hello world"
var short_string := the_string (3 .. *)    % short_string = "llo world"
put index (short_string, "w")                   % outputs 5


that wont quite work with my program
Cervantes wrote:

But that doesn't quite return what we want, does it? No, because the 'w' is the 7th character of 'the_string'.
code:

var offset := 3
var the_string := "Hello world"
var short_string := the_string (offset .. *)    % short_string = "llo world"
put index (short_string, "w") + offset - 1      % outputs 5


that wont work either.
Cervantes wrote:

We can compress that into one line (not including the declaration of 'offset'):
code:

put index (the_string (offset .. *), "w") + offset - 1


same here
Cervantes wrote:

xtreemboarder wrote:

also, how can i make it search for spaces (" ")?

Just like that.
code:

put index ("Hello World", " ")      % outputs 6


All code here untested.

then how come this doesn't work?
code:

var word:string
get word
put index (word, " ")

enter in 2 words seperated by spaces. the return value is 0... dont know why

anywho... im trying to figure out how to take 1 word from a string of words, and play around with it, re-order it. basically... i need to find where a certain word is,in between which positions, reorder it, and then move on to the next word.. thats why i need to figure out how to get index find spaces... i dont think theres any other way[/code]
 
TheOneTrueGod




PostPosted: Sun Jun 11, 2006 8:33 pm   Post subject: (No subject)

you need a get word :*. The normal get doesn't accept spaces. Though keep in mind that using

code:

get word :*


will allow input to exceed 255 characters, potentially causing an error.
 
Bored




PostPosted: Sun Jun 11, 2006 8:49 pm   Post subject: (No subject)

xtreemboarder wrote:
that wont work either.
Cervantes wrote:

We can compress that into one line (not including the declaration of 'offset'):
code:

put index (the_string (offset .. *), "w") + offset - 1


same here

xtreemboarder wrote:
anywho... im trying to figure out how to take 1 word from a string of words, and play around with it, re-order it. basically... i need to find where a certain word is,in between which positions, reorder it, and then move on to the next word.. thats why i need to figure out how to get index find spaces... i dont think theres any other way[/code]

I don't see how that would not work for what you need to do. Aslong as you know where to start the search from which for what your doing you should, that code will do the job. All you have to do is simply asign the starting position to offset. and replace "w" with what you are searching for.
 
xtreemboarder




PostPosted: Mon Jun 12, 2006 12:19 am   Post subject: (No subject)

hey now that i think about it... it will work... thanks man
 
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: