Author |
Message |
daneck13
|
Posted: 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
|
|
|
Insectoid
|
Posted: 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
|
Posted: 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. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
z_cross_fire
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
Posted: 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
|
|
|
Tony
|
Posted: 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!
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Insectoid
|
Posted: Wed Mar 16, 2011 2:24 pm Post subject: RE:counting the number of words in a string |
|
|
I win again Tony. |
|
|
|
|
|
Tony
|
|
|
|
|
daneck13
|
Posted: 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
|
Posted: 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"? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
daneck13
|
Posted: 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
|
Posted: 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? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
|