Computer Science Canada

How does this String Code work?

Author:  NickWaugh27 [ Wed Mar 28, 2018 1:11 pm ]
Post subject:  How does this String Code work?

I want to know how this code is working, I was instructed to use this as part of my code and I want to know if someone can help me out with having a better understanding of how this process works. Thank you in advance! I have all the variables stated at the top and I can post what they are if you want to know them, but I don't think they're needed, just let me know if you need them too

put "Enter your word"
get word


put "Enter a single letter pattern"
get pattern

size := length (pattern)

loop
pos := index (word, pattern)
exit when pos = 0
word := word (1 .. pos - 1) + word (pos + size .. *)
end loop
cls
put "Here is the word with the pattern removed: ", word

Author:  Insectoid [ Wed Mar 28, 2018 7:59 pm ]
Post subject:  RE:How does this String Code work?

Which part don't you understand?

Author:  NickWaugh27 [ Thu Mar 29, 2018 12:18 pm ]
Post subject:  RE:How does this String Code work?

Specifically I don't understand this part:

pos := index (word, pattern)
exit when pos = 0
word := word (1 .. pos - 1) + word (pos + size .. *)

I understand mostly how the index area works, but how does the last line work?

Author:  Insectoid [ Sun Apr 01, 2018 2:54 pm ]
Post subject:  RE:How does this String Code work?

The last line is just basic math. Word (a .. b) returns only the letters between indexes a and b inclusively. So 'hello world' (4, 8) returns 'lo wo'. When * is used that means 'the rest of the string'. The + sign combines two strings. So that whole line put together takes two pieces of word, glues them together, and then assigns that value to word.

Can you figure out why they used 1 .. pos-1 and pos + size .. *?


: