Discuss Scratch

TBNRSlater
Scratcher
8 posts

How can I count the number of times a letter appears in a variable?

So, I'm trying to code Wordle from scratch, in Scratch, and I want to detect how many times a letter appears in a variable.

Can anyone help me?

Last edited by TBNRSlater (Jan. 3, 2023 14:41:02)

groggs
Scratcher
100+ posts

How can I count the number of times a letter appears in a variable?

Hi there!

You can have a variable that changes each time your word contains the letter that you want. You can use the “contains?” block to detect if a string does contain a specific letter

<(var) contains [a]? :: operators> //Change "a" depending on the letter you want to find

then, if that variable contains the letter (eg, a) you change another variable by “1”, this variable could represent the times that the letter “a” appears in the string that the variable contains

change [foo v] by (1) //Each time the code above is true, change this by "1"

Please give more information in case that this didn't help you.

Last edited by groggs (Jan. 3, 2023 15:02:18)

medians
Scratcher
1000+ posts

How can I count the number of times a letter appears in a variable?

Try adding each letter to a list called “letters” or something like that, and if you're using 3.0, then find where it is in the list, and if it doesn't appear, stop it. If it is there, remove it but count it towards the total. Then repeat that.

If you're using 2.0, then use an index and add 1 to it until you get to it, but if it doesn't appear, then stop it. Remove it if it does and add 1 to the number of times it appears. Then repeat. (there is no item # of block in 2.0)
So to add all of the letters to the list just do this:
define letter count of (letter)
delete (all v) of [letters v]
repeat (length of (word))
add (letter ((length of [letters v] :: list) + (1)) of (word)) to [letters v] //this will add the 1st, then the 2nd, then the 3rd, then the 4th, and then the 5th letter (since at first the length is 0, then 1, then 2, then 3 then 4, and then 5 after it's done)
end
You'll need to do that in order to know which item needs to be removed.

Last edited by medians (Jan. 3, 2023 15:30:47)

PutneyCat
Scratcher
500+ posts

How can I count the number of times a letter appears in a variable?


set [word v] to [crate]
set [letter v] to [a]
set [counter v] to [0]
set [i v] to [1]
repeat (length of (word))
if <(letter (i) of (word)) = (letter)> then
change [counter v] by (1)
end
change [i v] by (1)
end

Last edited by PutneyCat (Jan. 3, 2023 15:55:07)

Oumuamua
Scratcher
1000+ posts

How can I count the number of times a letter appears in a variable?

medians
Scratcher
1000+ posts

How can I count the number of times a letter appears in a variable?

This code should work:

Note: index is only needed in 2.0.
Edit Note 2: If you want to also save all of the letters, then you can use another list called backup_letters or something and
do:
delete (all v) of [letters v]
delete (all v) of [backup letters v]
repeat (length of (word))
add (letter ((length of [letters v]) + (1)) of (word)) to [letters v]
add (letter ((length of [backup letters v]) + (1)) of (word)) to [backup letters v]
end

Last edited by medians (Jan. 5, 2023 04:32:18)

Powered by DjangoBB