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






code:

 for i : 1 .. length (word)
        count (i) := word (1 + length (word) - i)
        right (i) := word (i)

    end for

    for i : 1 .. length (word)
 
        if right (i) = count (i) then
            palindrome += 1
            if ord(word(i))>65 and ord (word(i))<90 then
            word := chr (ord(word(i))-32)
            if palindrome = length (word) then
                put "The word is a palindrome"
     
            end if
            end if
        end if
       
       
    end for


    if palindrome not= length (word) then
        put "The word is not a palindrome"
       
    end if 

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:

Turing:
put Str.Lower ("THIS IS ALL IN CAPS, OR IS IT?")


Or a bit more in depth:

Turing:
var foo : string := "bar"
var baz : string := "BaR"
put "foo = ", foo
put "baz = ", baz
put "Str.Lower (baz) = ", Str.Lower (baz)
put "foo = baz is ", foo = baz
put "foo = Str.Lower (baz) is ", foo = Str.Lower (baz)

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:

code:


 var word : string
    var lowercase : int


 % for loop that
    for i : 1 .. length (word)
        count (i) := word (1 + length (word) - i)
        right (i) := word (i)

    end for

    for i : 1 .. length (word)
 
        if right (i) = count (i) then
            palindrome += 1
            if ord(word(i))< 97 then
           
     
           lowercase := ord (word(i))+32
           put chr(ord (lowercase(i)))..
           

            if palindrome = length (word) then
                put "The word is a palindrome"
     
            end if
        end if
        end if



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.
Turing:

function Lower (str : string) : string
    var newString : string := ""
    for x : 1 .. length (str)
        if ord (str (x)) >= 65 and ord (str (x)) <= 90 then
            newString += chr (ord (str (x)) + 32)
        else
            newString += str (x)
        end if
    end for
    result newString
end Lower
put Lower ("ABLEH! ahHHHHHHh! FIZBUZZZZZ!!!")

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 Laughing but i just have one more question about this topic. now when i input the word lets say "ABBA" it tells me its a palindrome and its great cause thats what it suppose to do but than when i input "Abba" it tells me that its not a palindrome when it should tell me its a palindrome, i am gonna post my full code, its a big long but most of the lines you can skip reading cause they are straight forward


code:


    function Lower (str : string) : string
    var newString : string := ""
    for x : 1 .. length (str)
        if ord (str (x)) >= 65 and ord (str (x)) <= 90 then
            newString += chr (ord (str (x)) + 32)
        else
            newString += str (x)
        end if
    end for
    result newString
end Lower




loop


   
    var word : string
    var lowercase : int


    put "Please Enter a word: " ..
    get word
   
   



    var count : array 1 .. length (word) of string
    var right : array 1 .. length (word) of string
    var palindrome : int := 0


 
   

    for i : 1 .. length (word)
        count (i) := word (1 + length (word) - i)
        right (i) := word (i)

    end for
 
    for i : 1 .. length (word)
 
        if right (i) = count (i) then
            palindrome += 1
     word := Lower (word(i))
           

            if palindrome = length (word) then
                put "The word is a palindrome"
     
            end if
        end if
       
    end for



   
   
   
    if palindrome not= length (word) then
        put "The word is not a palindrome"
       
    end if 

   

end loop


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:
Turing:
word := Lower (word (i))
?
Take that line out, and place
Turing:
word := Lower (word)
after you get the value of word from the user. [/code]

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
code:

word := Lower (word (i))


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.

code:

word := Lower (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:
Turing:

    function Lower (str : string) : string
    var newString : string := ""
    for x : 1 .. length (str)
        if ord (str (x)) >= 65 and ord (str (x)) <= 90 then
            newString += chr (ord (str (x)) + 32)
        else
            newString += str (x)
        end if
    end for
    result newString
end Lower

loop
   
    var word : string
    var lowercase : int


    put "Please Enter a word: " ..
    get word
 
    word:= Lower (word)%this is important

    var count : array 1 .. length (word) of string
    var right : array 1 .. length (word) of string
    var palindrome : int := 0

    for i : 1 .. length (word)
        count (i) := word (1 + length (word) - i)
        right (i) := word (i)

    end for
 
    for i : 1 .. length (word)
        if right (i) = count (i) then
            palindrome += 1
    % word := Lower (word(i))
            if palindrome = length (word) then
                put "The word is a palindrome"
            end if
        end if
    end for

    if palindrome not= length (word) then
        put "The word is not a palindrome"
    end if

end loop

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.



code:

   function Lower (str : string) : string
    var newString : string := ""
    for x : 1 .. length (str)
        if ord (str (x)) >= 65 and ord (str (x)) <= 90 then
            newString += chr (ord (str (x)) + 32)
        else
            newString += str (x)
        end if
    end for
    result newString
end Lower

loop
   
    var word : string
    var lowercase : int


    put "Please Enter a word: " ..
    get word
  put chr(32)
    word:= Lower (word)

    var count : array 1 .. length (word) of string
    var right : array 1 .. length (word) of string
    var palindrome : int := 0

    for i : 1 .. length (word)
        count (i) := word (1 + length (word) - i)
        right (i) := word (i)
     
end if
    end for
 
    for i : 1 .. length (word)
        if right (i) = count (i) then
            palindrome += 1
 
            if palindrome = length (word) then
                put "The word is a palindrome"
            end if
        end if
    end for

    if palindrome not= length (word) then
        put "The word is not a palindrome"
    end if

end loop



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).
code:
var Alpha : string := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var ALPHA : string := "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"
var lALPHA : string := "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"

function UPPER (upper : boolean, w, w2 : string) : string
    if length (w) < 1 then
        result w2
    end if
    if upper then
        result UPPER (true, w (2 .. *), w2 + ALPHA (index (Alpha, w (1))))
    else
        result UPPER (false, w (2 .. *), w2 + lALPHA (index (Alpha, w (1))))
    end if
end UPPER

put UPPER (true, "Hello", "")
put UPPER (false, "Hello", "")


Just upper

code:
var Alpha : string := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var ALPHA : string := "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ"

function UPPER (w, w2 : string) : string
    if length (w) < 1 then
        result w2
    end if
    result UPPER (w (2 .. *), w2 + ALPHA (index (Alpha, w (1))))
end UPPER

put UPPER ("Hello", "")


Actual Palindrome part;
code:
var Word : string := "Level"
var bWord : string := "" %backwards, punny?

for decreasing i : length (Word) .. 1
    bWord += Word (i)
end for

if UPPER (bWord, "") = UPPER (Word, "") then
    put "This word is a palindrome! Omgz0rz!!11!one!!two2!1Elven!11!!!"
else
    put "Nay, Palindrome ne pas!"
end if

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.
Turing:
fcn toUpper (s : string) : string
    if length (s) > 0 and s (1) >= 'a' and s (1) <= 'z' then
        result chr (ord (s (1)) - 32) + toUpper (s (2 .. *))
    end if
    result ""
end toUpper


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.

Turing:
fcn reverse (s : string) : string
    if length (s) > 0 then
        result s (*) + reverse (s (1 .. * -1))
    end if
    result ""
end reverse

fcn isPalindrome (s : string) : boolean
    var upperString := toUpper (s)
    result upperString = reverse (upperString)
end isPalindrome


: