Computer Science Canada Treat Lower case letters the same as Upper case letters using ASCII |
Author: | dmitrip [ Sat Mar 17, 2007 4:09 pm ] |
Post subject: | Treat Lower case letters the same as Upper case letters using ASCII |
hello i am writing a program that has the user input a word and it outputs if the word is a palindrome(reads the same backwards and forwards) or if it is not a palindrome. Now what i am trying to make it do is treat upper case letters the same as lower case letters, so for example chr(97) = chr(65) which means that 'a' is the same letter as 'A' so how could i get the program to treat the letters the same no matter if its upper case or lower case. thank you very much. |
Author: | CodeMonkey2000 [ Sat Mar 17, 2007 4:25 pm ] |
Post subject: | RE:Treat Lower case letters the same as Upper case letters using ASCII |
First look at the ASCII chart (it's some where in the turing reference). Look at the ASCII for lower case letters, and the higher case. Notice a pattern? All the higher case letters are 32 characters lower than their higher case counter part (i.e. chr(65)='A' and chr(97)='a' and the difference is 32 ). Use this idea to solve the problem. |
Author: | [Gandalf] [ Sat Mar 17, 2007 5:50 pm ] |
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII |
Or you can use the Str module built into Turing and convert all your words to either all caps (Str.Upper("hi")) or all lower case (Str.Lower("HI")). That way when you make your palindrome checks it will treat the letters the same whether or not they are in caps. |
Author: | dmitrip [ Sun Mar 18, 2007 1:43 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
this is the bit of code from my program that should convert all the letters into lowercase letters but it but it isnt working. help will be very appreciteated, and i tried the above but i been trying for an hour now and this is only as far as i got. thank you
|
Author: | [Gandalf] [ Sun Mar 18, 2007 2:41 pm ] | ||||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||||
While it may be good to know, you don't need to work with ord() and chr() to solve your problem. The Str.Lower() function returns whichever string it is given in all lower case. Try it:
Or a bit more in depth:
|
Author: | ericfourfour [ Sun Mar 18, 2007 2:41 pm ] |
Post subject: | RE:Treat Lower case letters the same as Upper case letters using ASCII |
dmitrip look up at the post above your last one. The simplest answer is right there. Edit: err... posted exactly the same time as gandalf. |
Author: | dmitrip [ Sun Mar 18, 2007 2:53 pm ] |
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII |
the thing is i am using turing for windows by holt softwares and it give me an error that says: 'Lower' is not in the export list of 'Str' |
Author: | dmitrip [ Sun Mar 18, 2007 3:03 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
i appreciate everybody trying to help me, i started playing around with the code a little and i wrote it in a little bit diffrenet way here it is:
this is the same code as above but i rewrote it in a different way , but it gives me an error in the line put chr(ord (lowercase(i))).. anybody know what the problem might be, if needed i can post the full code. thanks alot everybody |
Author: | CodeMonkey2000 [ Sun Mar 18, 2007 3:28 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
I think you get the error because your version of turing doesn't have Str.Lower defined (Iknow mine doesn't). I wrote a Lower function a while back for my compsci class.
|
Author: | dmitrip [ Sun Mar 18, 2007 5:04 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
CodeMonkey2000 i really appreciate your help and i like your sentence on the bottom of your tag ![]()
thank you very much for all the help! |
Author: | Clayton [ Sun Mar 18, 2007 5:26 pm ] |
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII |
I think you're trying to complicate this a bit much. Just have a for loop that checks each letter from the outside in, 1 character deeper for each iteration. If the two characters match, go on to the next iteration of the loop, otherwise, exit the loop. Ideally this will be in a function that returns a boolean value to ease the coding of the main line. |
Author: | CodeMonkey2000 [ Sun Mar 18, 2007 5:34 pm ] | ||||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||||
What is the significance of this:
Take that line out, and place
|
Author: | Clayton [ Sun Mar 18, 2007 5:45 pm ] | ||||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||||
Check out the String Manipulation tutorial that is in the Turing Walkthrough to answer any other questions after reading this. Basically, what
means is that you are setting word to equal the lowercase equivalent of character "i" in the string word. A string is more or less an array of characters mashed together to create a string. Following this, using word (i) makes sense because word is basically an array of characters, so word (i) is accessing the ith element of word.
is actually putting the entire string word through the function Lower in this case. Not just a character, the whole thing. EDIT: I read that as a question as what the difference between the two was. |
Author: | dmitrip [ Sun Mar 18, 2007 5:46 pm ] |
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII |
ok i took it out but that still doenst solve my problem, if i type in "Abba" for word it still says its not a palindrome when it is thanks alot |
Author: | CodeMonkey2000 [ Sun Mar 18, 2007 5:54 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
By the way, you can simplify this code A LOT. Anyway try this and see if it works:
|
Author: | dmitrip [ Sun Mar 18, 2007 6:23 pm ] | ||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||
that did it thank you sooo much CodeMonkey2000 i really appreciate it! Freakman Edit: Try and post questions for similiar programs in the same thread. Thanks, it just keeps things tidy. how could i make my program ignore spaces and punctuations, i know i have to use chr(32) something but i am not sure where to use it. my program asks for an input from the user, and it tells the user whether the program is a palindrome or not.
thanks alot |
Author: | ericfourfour [ Mon Mar 19, 2007 3:13 pm ] |
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII |
You think way to much. The character for space in pretty much all programming languages is ' '. In Turing it can also be " ". The character for tab is '\t'. In Turing it can also be "\t". The character for newline is '\n'. In Turing it can also be "\n". If you want to find all punctuation you can simply check if it isn't a number or a letter. Punctuation is not between 1 and 9. Punctuation is not between a and z. Punctuation is not between A and Z. There isn't any need to confuse yourself with ord and chr when dealing with a simple problem like this. |
Author: | Flikerator [ Thu Mar 22, 2007 11:09 pm ] | ||||||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||||||
Something I did when I first learned about recursion (the bottom one, the top one is modded just now) to get a hang of it. Pretty simple; makes all letters upper or lower. Just a different method you might be interested in...maybe not. Lower or Upper (false or true respectivly).
Just upper
Actual Palindrome part;
|
Author: | ericfourfour [ Fri Mar 23, 2007 2:37 pm ] | ||||
Post subject: | Re: Treat Lower case letters the same as Upper case letters using ASCII | ||||
Flikerator, you are using way to many parameters. You should only need one. There is also no need to list every letter.
The only constant I used in this is 32 because that is the difference between the upper and lowercase letter ascii values. Here are two other functions. There is room for optimizations in the isPalindrome function.
|