Computer Science Canada

counting the number of words in a string

Author:  daneck13 [ 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

Author:  Insectoid [ 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.

Author:  Tony [ 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.

Author:  z_cross_fire [ 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)....

Author:  Zren [ 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.

Author:  DtY [ 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.

Author:  daneck13 [ 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

Author:  Insectoid [ 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.

Author:  Tony [ 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!

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

I win again Tony.

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

Extra karma point for you.

Author:  daneck13 [ 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

Author:  Tony [ 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"?

Author:  daneck13 [ 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>

Author:  Tony [ 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?

Author:  daneck13 [ Thu Mar 17, 2011 12:14 pm ]
Post subject:  Re: counting the number of words in a string

yes but then what do i do to change it from being always false

Author:  Insectoid [ Thu Mar 17, 2011 12:22 pm ]
Post subject:  RE:counting the number of words in a string

code:
if words(i..x)=" "


You're specifying a range here. I dunno how Turing would handle this, but it looks like 'If this section of this string is a space' rather than 'if this character of this string is a space'.

For example, if our string is 'Hello world', and x is 6, Turing will compare 'Hello '* to ' '. See how it isn't equal?

*This may vary depending on weather or not Turing zero-indexes strings. I don't remember.

Author:  Tony [ Thu Mar 17, 2011 12:23 pm ]
Post subject:  RE:counting the number of words in a string

You change it to something that has a chance at being true at least sometimes.

The problem here is that you are asking (in a way of writing code) for a computer to do something that is different from what you _think_ you are asking. It might be subtle and you might have to print out some actual values to see what's going on. The hint is that it's always false, your job is to figure out why.


: