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

Username:   Password: 
 RegisterRegister   
 counting the number of words in a string
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
daneck13




PostPosted: Tue Mar 15, 2011 7:05 pm   Post subject: counting the number of words in a string

Write a Turing program to read a series of lines and count the
number of words read before the word "stop"is reached.

i need help with the counting of the words because i dont understand how to separate the string into words
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Tue Mar 15, 2011 7:36 pm   Post subject: RE:counting the number of words in a string

Assuming a word is defined as a sequence of characters not including a space, just search for spaces. When you find one, you've found a word.
Tony




PostPosted: Tue Mar 15, 2011 7:37 pm   Post subject: Re: counting the number of words in a string

daneck13 @ Tue Mar 15, 2011 7:05 pm wrote:
i need help with the counting of the words because i dont understand how to separate the string into words

If you look at the above sentence, you might notice that words have a delimiter, typically a space, separating them.

edit: you win this round Insectoid.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
z_cross_fire




PostPosted: Tue Mar 15, 2011 8:29 pm   Post subject: RE:counting the number of words in a string

Use the split function and keep space as delimiter (as said above)....
Zren




PostPosted: Tue Mar 15, 2011 9:00 pm   Post subject: Re: RE:counting the number of words in a string

z_cross_fire @ Tue Mar 15, 2011 8:29 pm wrote:
Use the split function and keep space as delimiter (as said above)....

Turing doesn't have a built in split function. You would have to write one yourself. In this case, writing the entire split function isn't needed as you only need to scan each string.
DtY




PostPosted: Wed Mar 16, 2011 1:41 pm   Post subject: Re: RE:counting the number of words in a string

Insectoid @ Tue Mar 15, 2011 7:36 pm wrote:
Assuming a word is defined as a sequence of characters not including a space, just search for spaces. When you find one, you've found a word.
Not completely. "This sentence has 7 spaces but 8 words".

When you do this, keep in mind we put a space *between* words. There will always be one less space between words than words in the sentence.

e; Multiple consecutive spaces also does not mean another word, but you probably don't need to worry about that.
daneck13




PostPosted: Wed Mar 16, 2011 2:10 pm   Post subject: RE:counting the number of words in a string

how would i write a split function or skan for spaces
Insectoid




PostPosted: Wed Mar 16, 2011 2:23 pm   Post subject: RE:counting the number of words in a string

You know how to loop over a string? Do that. Check if the character at that spot is a space. There you go.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Mar 16, 2011 2:23 pm   Post subject: Re: RE:counting the number of words in a string

daneck13 @ Wed Mar 16, 2011 2:10 pm wrote:
how would i write a split function or skan for spaces

code:

is this letter a space?
no - check next letter
yes - found a space!
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Wed Mar 16, 2011 2:24 pm   Post subject: RE:counting the number of words in a string

I win again Tony.
Tony




PostPosted: Wed Mar 16, 2011 2:35 pm   Post subject: RE:counting the number of words in a string

Extra karma point for you.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
daneck13




PostPosted: Wed Mar 16, 2011 2:40 pm   Post subject: Re: counting the number of words in a string

put"this program will count the number of words in a string"
put""
var words:string



get words:*
var x:int:=1
var count:= length(words)
var xx:int

for i :1..count
put i
x:=i+1
if words(i..x)=" " then
put x=1+1

else put ""


end if
end for




so heres what i have it dosent work thou
Tony




PostPosted: Wed Mar 16, 2011 2:56 pm   Post subject: RE:counting the number of words in a string

Well _I_ see where the problem is, but can _you_ explain in what way it "doesn't work"?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
daneck13




PostPosted: Wed Mar 16, 2011 3:00 pm   Post subject: Re: counting the number of words in a string

well i have i put the "count" so i know what step its on but it puts a space(return) instead of searching for the space and therefor dosent count how many words there are>
Tony




PostPosted: Wed Mar 16, 2011 3:05 pm   Post subject: RE:counting the number of words in a string

If the program _always_ takes the else put "" path, can you then conclude that words(i..x)=" " part is _always_ false?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 18 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: