Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Treat Lower case letters the same as Upper case letters using ASCII
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dmitrip




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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.
[Gandalf]




PostPosted: 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.
dmitrip




PostPosted: 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 
[Gandalf]




PostPosted: 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)
ericfourfour




PostPosted: 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.
dmitrip




PostPosted: 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'
dmitrip




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: 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!!!")
dmitrip




PostPosted: 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!
Clayton




PostPosted: 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.
CodeMonkey2000




PostPosted: 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]
Clayton




PostPosted: 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.
dmitrip




PostPosted: 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
CodeMonkey2000




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: