Computer Science Canada

Sorting a string

Author:  cat8864 [ Thu Nov 20, 2008 1:58 pm ]
Post subject:  Sorting a string

I need to create a program that takes a string of user input and sorts according to vowel, consonant, number and other. I managed to get it to work partially. The vowels and consonants get sorted fine but anything else gets place with the consonants. Any attempt to modify it just results in one of many error messages that say the results are too long or something exceeds the boundaries.

Here's my program so far:

Turing:
var word : string
const vowels := "aeiou"
const consonants := "bcdfghjklmnpqrstvwxyz"
const numbers := "1234567890"
const other := "~`!@#$%^&*()_+-={}[]:;'?/><,."

put "Enter a string"

loop
    get word
    exit when word = "*"

    var newword := ""
    var newword2 := ""
    var newword3 := ""
    var newword4 := ""

    var size := length (word)

    for i : 1 .. size
        if index (vowels, word (i)) = 0 then
            newword := newword + word (i)


        elsif index (consonants, word (i)) = 0 then
            newword2 := newword2 + word (i)


        elsif index (numbers, word (i)) = 0 then
            newword3 := newword3 + word (i)


        elsif index (other, word (i)) = 0 then
            newword4 := newword4 + word (i)

        end if
    end for

    put "consonants  ", newword
    put "vowels      ", newword2
    put "numbers     ", newword3
    put "other       ", newword4

    % var posv := index (word (i), vowels)
    % var posc := index (word (i), consonants)
    % var posn := index (word (i), numbers)
    % var poso := index (word (i), other)

    % word := word (1 .. posv - 1) + word (posc + size .. *)
    % word := word (1 .. posc - 1) + word (posc + size .. *)
    % word := word (1 .. posn - 1) + word (posn + size .. *)
    % word := word (1 .. poso - 1) + word (poso + size .. *)

end loop


Mod Edit: Instead of color tags for code, use syntax tags. Thanks Smile
code:
[syntax="Turing"]Code Here[/syntax]

Author:  The_Bean [ Thu Nov 20, 2008 7:00 pm ]
Post subject:  Re: Sorting a string

Simply changing the =0 to >0 should do it.

Author:  cat8864 [ Fri Nov 21, 2008 1:01 pm ]
Post subject:  Re: Sorting a string

Thanks! But why '>' instead of '='? Is it because the length changes each time or something?

Author:  HellblazerX [ Fri Nov 21, 2008 1:06 pm ]
Post subject:  RE:Sorting a string

Because index returns the position of the character.

Author:  cat8864 [ Fri Nov 21, 2008 1:39 pm ]
Post subject:  Re: Sorting a string

So because I had "= 0" it didn't read the rest of the if statement


: