
-----------------------------------
daneck13
Tue Mar 15, 2011 7:05 pm

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

-----------------------------------
Insectoid
Tue Mar 15, 2011 7:36 pm

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
Tue Mar 15, 2011 7:37 pm

Re: counting the number of words in a string
-----------------------------------
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.

-----------------------------------
z_cross_fire
Tue Mar 15, 2011 8:29 pm

RE:counting the number of words in a string
-----------------------------------
Use the split function and keep space as delimiter (as said above)....

-----------------------------------
Zren
Tue Mar 15, 2011 9:00 pm

Re: RE:counting the number of words in a string
-----------------------------------
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
Wed Mar 16, 2011 1:41 pm

Re: 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.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
Wed Mar 16, 2011 2:10 pm

RE:counting the number of words in a string
-----------------------------------
how would i write a split function or skan for spaces

-----------------------------------
Insectoid
Wed Mar 16, 2011 2:23 pm

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.

-----------------------------------
Tony
Wed Mar 16, 2011 2:23 pm

Re: RE:counting the number of words in a string
-----------------------------------
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!
[/code]

-----------------------------------
Insectoid
Wed Mar 16, 2011 2:24 pm

RE:counting the number of words in a string
-----------------------------------
I win again Tony.

-----------------------------------
Tony
Wed Mar 16, 2011 2:35 pm

RE:counting the number of words in a string
-----------------------------------
Extra karma point for you.

-----------------------------------
daneck13
Wed Mar 16, 2011 2:40 pm

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
Wed Mar 16, 2011 2:56 pm

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"?

-----------------------------------
daneck13
Wed Mar 16, 2011 3:00 pm

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
Wed Mar 16, 2011 3:05 pm

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?

-----------------------------------
daneck13
Thu Mar 17, 2011 12:14 pm

Re: counting the number of words in a string
-----------------------------------
yes but then what do i do to change it from being always false

-----------------------------------
Insectoid
Thu Mar 17, 2011 12:22 pm

RE:counting the number of words in a string
-----------------------------------
[code] if words(i..x)=" "[/code]

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.

-----------------------------------
Tony
Thu Mar 17, 2011 12:23 pm

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.
