Sorting Arrays in Alphabetical Order, capitals and lowercase
Author |
Message |
reymalile
|
Posted: Thu May 19, 2011 8:40 am Post subject: Sorting Arrays in Alphabetical Order, capitals and lowercase |
|
|
What is it you are trying to achieve?
I am trying to sort arrays in alphabetical order
What is the problem you are having?
When I am sorting capital and lowercases, i change the capitals into a lowercase, sort them but then I cant change the capitals I changed to lowercase, back to capital.
Describe what you have tried to solve this problem
I tried using two arrays however theres a problem when sorting
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
var firstName : array 1 .. 40 of string
var HowMany : int
var first : string (1)
var lowercase : string := ""
var change : flexible array 1 .. 20 of string
proc total
var n : flexible array 1 .. 40 of string
var x : flexible array 1 .. 40 of string
var ctr : int
ctr := 0
loop
ctr + = 1
HowMany := ctr
put "Enter name (xxx to exit): "
get n (ctr )
if ord (n (ctr ) (1)) >= 65 and ord (n (ctr ) (1)) <= 90 then
var i : int := 0
i + = 1
x (ctr ) := n (ctr )
n (ctr ) := Str.Lower (n (ctr ) (1)) + n (ctr ) (2 .. length (n (ctr )))
elsif n (ctr ) = "xxx" or n (ctr ) = "XXX" then
new n, (ctr - 1)
HowMany := ctr - 1
exit
end if
end loop
var temp, temp1 : string
for ctr1 : 1 .. HowMany - 1
for ctr2 : ctr1 + 1 .. HowMany
if n (ctr1 ) > n (ctr2 ) then
temp := n (ctr1 )
n (ctr1 ) := n (ctr2 )
n (ctr2 ) := temp
end if
end for
end for
for ctr1 : 1 .. upper (n )
put n (ctr1 )
end for
end total
total
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu May 19, 2011 11:01 am Post subject: RE:Sorting Arrays in Alphabetical Order, capitals and lowercase |
|
|
Str.Upper does the opposite of Str.Lower, but... since both "abc" and "Abc" both look like "abc" when you lower the first character, you wouldn't know how to restore the word without additional information.
Since you are trying to implement a custom definition of a sort, perhaps the build-in
code: |
"full_word" <compare> "ANOTHER_word"
|
is not the right way to go (since the comparison operators such as > are already defined as something else). |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Thu May 19, 2011 12:00 pm Post subject: RE:Sorting Arrays in Alphabetical Order, capitals and lowercase |
|
|
You don't actually need to store lower-case values of upper-case characters. Since str.lower returns a value and doesn't actually change the parameter, you can use it in a condition.
code: |
if Str.Lower (string1) > Str.Lower (string2) then
swap (string1, string2)
end if
|
|
|
|
|
|
![](images/spacer.gif) |
reymalile
|
Posted: Fri May 20, 2011 8:07 am Post subject: RE:Sorting Arrays in Alphabetical Order, capitals and lowercase |
|
|
thanks insectoid, your code worked |
|
|
|
|
![](images/spacer.gif) |
|
|