Discuss Scratch

DogeTheKitten
Scratcher
54 posts

Word counter

How do I find the number of words in an answer, like the:
ask [] and wait
block? like if the answer was “hello world”, how would I count the words?

Last edited by DogeTheKitten (Feb. 28, 2018 17:37:50)

KingOfAwesome58219
Scratcher
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:
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
Scratcher
1000+ posts

Word counter

here's what I did
when green flag clicked
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
it 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)

Last edited by gor-dee (Feb. 28, 2018 21:34:20)

Dabman2392
Scratcher
100+ posts

Word counter

Last edited by Dabman2392 (Dec. 17, 2020 17:24:31)

potatohead_1011
Scratcher
100+ posts

Word counter

Please

Dabman2392 wrote:

please don't necropost
Dabman2392
Scratcher
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)

Dabman2392
Scratcher
100+ posts

Word counter

Otherwise, use the other methods
Dabman2392
Scratcher
100+ posts

Word counter

and sorry, i never checked the time this was posted.

Powered by DjangoBB