Computer Science Canada

Editing strings to delete, change and count parts of words

Author:  BigmanSucksBalls [ Wed May 14, 2008 3:29 pm ]
Post subject:  Editing strings to delete, change and count parts of words

I have to get this program that prompts the user for a word and a letter, then displays the word without the letter. I could get it right only with one letter.
ex: if the word is 'banana' and the letter is 'n', the output would be baaa.
if there is no letter in the designated word then the word would stay the same.

I can't seem to get the program to work when i use more than one letter to delete. such as if the word is 'banana' and i want to delete 'an' to get 'ba', it wouldn't work.
I need this part in order to replace the letter, so if i wanted to replace 'a' with 'x', the word 'banana' would be 'bxnxnx'.
thanks.

This is my code so far... note that it only works with one letter instead of a group of letters. and the input word has to be only 1 word, not 2 or more.




put "Enter a word:"
get word
put "Enter a letter:"
get letter

var newWord : string := ""

for i : 1 .. length (word)
if index (letter, word (i)) = 0 then % Delete letter
newWord := newWord + word (i)
end if
end for

put "The word ", word, " becomes ", newWord





This is my attempt to get more than one word. It doesn't really work. and yes i did declare all the variables.

newWord := ""


for i : 1 .. length (word) - length (letter) + 1
% Delete Letter
if word (i .. i + (length (letter) - 1)) not= letter and counter = 0 then
newWord := newWord + word (i)
counter := 0
elsif word (i .. i + (length (letter) - 1)) = letter and counter = 0 then
counter := length(word)
elsif word (i .. i + (length (letter) - 1)) not= letter and counter = length(word) then
counter := length(word) - 1

end if
end for

Author:  gitoxa [ Wed May 14, 2008 4:54 pm ]
Post subject:  Re: Editing strings to delete, change and count parts of words

First of all, I'd recommend not using the "index" command. Fiddle around with these lines of code for a few minutes.

code:
var Word : string
Word := "Hello!"

put Word
put Word (1)
put Word (4)
put Word (1 .. 4)
put Word (1 .. *)
put Word (1 .. *-1)

for i : 1 .. length(Word)
    put Word(i)
end for

Author:  BigmanSucksBalls [ Wed May 14, 2008 9:11 pm ]
Post subject:  RE:Editing strings to delete, change and count parts of words

i kno how to work with strings and such but getting just part of a word's pattern is harder than it seems...
i tried calculating many ways to do it but somehow turing just doesnt like me.

Author:  riveryu [ Wed May 14, 2008 10:23 pm ]
Post subject:  Re: Editing strings to delete, change and count parts of words

I agree with gitoxa's advice, and he's giving a good hint. Remember you can add strings and "" also count as a string (with nothing).
heres just an example to show ways to manipulate a string:
Turing:
var inputWord : string := "ABABAB"
var output : string := ""
for i : 1 .. length (inputWord)
    if i mod 2 = 0 then             %mod 2 = 0 just means if i is divisible by 2 then....
        output += inputWord (i)  %the output string all the even number letter of the input (2),(4),(6)
    end if
end for
put output


: