Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Word counter
- DogeTheKitten
-
54 posts
Word counter
How do I find the number of words in an answer, like the:
ask [] and waitblock? like if the answer was “hello world”, how would I count the words?
Last edited by DogeTheKitten (Feb. 28, 2018 17:37:50)
- KingOfAwesome58219
-
1000+ posts
Word counter
Go through a string, adding every letter to the same line. When you encounter a space, add a new line and start adding to that instead. Remove all blank values in the list, and just use the length of the list.
Here's an example:
Here's an example:
define wordCount = number of words in (input)
delete (all v) of [count v]
set [i v] to (0) // keeps track of what letter we're at
repeat (length of (input))
change [i v] by (1)
if <(letter (i) of (input)) = [ ]> then // that's a space (" ")
add [] to [count v] // this is blank, not a space
else
replace item (last v) of [count v] with (join (item (last v) of [count v])(letter (i) of (input)))
end
end
set [i v] to (1)
repeat (length of [count v])
if <(item (i) of [count v]) = [ ]> then // that's a space
delete (i) of [count v]
end
change [i v] by (1)
end
set [wordCount v] to (length of [count v]
Last edited by KingOfAwesome58219 (Feb. 28, 2018 18:38:37)
- gor-dee
-
1000+ posts
Word counter
here's what I did
when green flag clickedit doesn't separate the words and put them in a list, just gives you a word count. It should work even if the answer has multiple spaces at the beginning, middle or end which I think might break the example above (although I haven't tested it)
ask [What's your name?] and wait
set [word count v] to [0]
set [i v] to [1]
repeat until <<not <(letter (i) of (answer)) = [ ]>> or <(i) = (length of (answer))>>
change [i v] by (1)
end
repeat until <(i) = (length of (answer))>
repeat until <<(letter (i) of (answer)) = [ ]> or <(i) = (length of (answer))>>
change [i v] by (1)
end
change [word count v] by (1)
repeat until <<not <(letter (i) of (answer)) = [ ]>> or <(i) = (length of (answer))>>
change [i v] by (1)
end
end
Last edited by gor-dee (Feb. 28, 2018 21:34:20)
- Dabman2392
-
100+ posts
Word counter
If you are using it to make noises for the amount of characters that there are in an answer, then, use this:
repeat (length of (answer))
play sound [pop]
end
Last edited by Dabman2392 (Dec. 17, 2020 17:45:47)
- Discussion Forums
- » Help with Scripts
-
» Word counter