How to alphabeticize two inputted words when one of the words is capitalized?
Author |
Message |
fruit7654321
|
Posted: Sun Jun 19, 2011 6:42 pm Post subject: How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
What is it you are trying to achieve?
Hey guys. This is my first time at this forum and I'm still a newbie at Turing.
Anyways, the question I'm having trouble with is from my homework at school.
The exact questions is: "5. Write a program that asks for a word and then for a second word. The two words should then be printed in alphabetical order. Be sure to check that your program works correctly. "
If you're reading this, thanks for your time.
What is the problem you are having?
My problem, aforementioned in my title, is that the program works perfectly until the cases of the words are different.
Describe what you have tried to solve this problem
I've tried to fix this problem by first detecting a capitalized letter and changing it, but I don't know how to change it.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Here's my code:
Turing: |
var first, second : string
put "Enter the first word: " ..
get first
put "Enter the second word: " ..
get second
if (first < second) then
put first, " ", second
else
put second, " ", first
end if
|
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) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sun Jun 19, 2011 6:56 pm Post subject: RE:How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
That's because capital letters are different values than their lowercase brethren. Completely different sections of the chart actually:
Basically you want to either deal with just uppercase or lowercase for both strings.
Str.Lower
Str.Upper |
|
|
|
|
![](images/spacer.gif) |
fruit7654321
|
Posted: Sun Jun 19, 2011 7:01 pm Post subject: Re: How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
Thanks for your reply. I understand it now
Here's my new code, which works:
Turing: |
var one, two: string
put "What is your first word? "..
get one
put "What is your second word? "..
get two
if Str.Lower(one )>two then
put two, " ", one
elsif Str.Lower(two )>one then
put one, " ", two
end if
|
Now my questions is: Is there a way to do this without using these keywords? And what if there were an undefined number of words to alphabetize? How would i do that? |
|
|
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sun Jun 19, 2011 7:08 pm Post subject: RE:How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
one = a
two = B
Broken.
As for your second question. You'd first have all the words in an array. Then you'd sort it.
There are tons of ways to sort, but focus on the Bubble sort as it's the easiest to learn (and a good stepping stone). |
|
|
|
|
![](images/spacer.gif) |
fruit7654321
|
Posted: Sun Jun 19, 2011 7:25 pm Post subject: Re: How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
Okay, so i made it so that both strings would always be lowercase.
Thanks for your help. |
|
|
|
|
![](images/spacer.gif) |
RandomLetters
|
Posted: Sun Jun 19, 2011 9:51 pm Post subject: RE:How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
Have you considered
one = a
two = a |
|
|
|
|
![](images/spacer.gif) |
fruit7654321
|
Posted: Mon Jun 20, 2011 5:53 pm Post subject: Re: How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
Wouldn't making it >= and <= fix that? |
|
|
|
|
![](images/spacer.gif) |
RandomLetters
|
Posted: Mon Jun 20, 2011 5:55 pm Post subject: RE:How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
Yes, but you don't need two if statements. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
fruit7654321
|
Posted: Mon Jun 20, 2011 5:59 pm Post subject: Re: How to alphabeticize two inputted words when one of the words is capitalized? |
|
|
code: |
var one, two:string
put "What is your first word? "..
get one
put "What is your second word? "..
get two
if Str.Lower(one)>Str.Lower(two) then
put two, " ", one
else
put one, " ", two
end if
|
|
|
|
|
|
![](images/spacer.gif) |
|
|