Computer Science Canada

whitespacing

Author:  ofcourseitsdan [ Thu Feb 23, 2012 10:49 am ]
Post subject:  whitespacing

What is it you are trying to achieve?
Im trying to remove spaces in a string


What is the problem you are having?
removing spaces


Please specify what version of Turing you are using
4.1.1

Author:  Dreadnought [ Thu Feb 23, 2012 12:56 pm ]
Post subject:  Re: whitespacing

What have you tried? What do you consider whitespace?

Author:  Tony [ Thu Feb 23, 2012 4:19 pm ]
Post subject:  RE:whitespacing

What kind of a problem are you having with this? (trying to find where whitespace is; removing a character at a known location; etc)

Author:  ofcourseitsdan [ Fri Feb 24, 2012 9:51 am ]
Post subject:  Re: whitespacing

Dreadnought @ Thu Feb 23, 2012 12:56 pm wrote:
What have you tried? What do you consider whitespace?


if you read my problem it says i cant remove spaces. ive used this code but it outputs the letters on different lines if the word has spaces. e.g "h e y" outputs

h
e
y

Turing:
var word : string
var newword : string := ""

procedure remove_spaces (user_word : string)
    newword := ""

    for x : 1 .. length (word)
        if index (" ", word (x)) = 0 then
            newword += word (x) + ""
        end if
    end for


    put newword
end remove_spaces

loop
    get word

    remove_spaces (word)
end loop

Author:  Raknarg [ Fri Feb 24, 2012 11:08 am ]
Post subject:  RE:whitespacing

newword += word (x) + ""

get rid of the + ""

Author:  [Gandalf] [ Fri Feb 24, 2012 5:35 pm ]
Post subject:  RE:whitespacing

By default, get word will treat any whitespace as the end of the input. To include whitespace, up to the maxium string length, use get word : *. The asterisk is a wildcard meaning take as many characters as possible, however it could be replaced with a number specifying the maximum characters to include in the input.

Also, one suggestion to improve your program: local variables are always better than global variables. For example:
Turing:
procedure remove_spaces (user_word : string)
    var newword : string := "" % local, also no redundant assignment

    for x : 1 .. length (word)
        if index (" ", word (x)) = 0 then
            newword += word (x) % unnecessary + "" as mentioned
        end if
    end for


    put newword
end remove_spaces

loop
    var word : string % local, and passed to remove_spaces as a parameter
    get word : * % include whitespace

    remove_spaces (word)
end loop

Author:  Raknarg [ Fri Feb 24, 2012 8:06 pm ]
Post subject:  RE:whitespacing

Right. Didn't think that one through.


: