943,947 Members | Top Members by Rank

Ad:
Nov 10th, 2009
0

Scheme procedure to capitalize a string

Expand Post »
Hello, and thank you for helping me today! I am working on a scheme procedure that takes a string as its input, capitalizes every letter in the string, and then prints out the result. This is a homework assignment, and we are supposed to use another procedure that we wrote earlier on, which capitalizes a single character entered by the user. This procedure, which I wrote and which works fine, is included at the top of my current procedure. Right now, I have the following:

; defining the procedure char_toupper to convert a lower case character to upper case
(define char_toupper (lambda (myChar)
                       ; defining x as the ASCII value of myChar
                       (define x (char->integer myChar))
                       ; defining y as x-32
                       (define y (- x 32))
                       ; if statement for while x is less than 91 (already uppercase)
                       (if (< x 91)
                            ; if it is already uppercase, just display it
                            (display myChar)
                            ; otherwise, if x is greater than 96 (lowercase)
                            (if (> x 96)
                                ; then display the character equivalent to the ASCII value given by y
                                (display (integer->char y))
                            )
                        )
                       )
)

(define string_toupper (lambda (myString newString i)       
                         (if (< i (string-length myString))
                             (string_toupper myString (string-append newString (char_toupper (string-ref myString i))) (+ 1 i))
                         )
                         
                         (display newString)
                       )
)

(string_toupper (read) "" 0)

But it is not working. I get an error when I run the procedure, which says this: "string-append: expects type <string> as 2nd argument, given: #<void>; other arguments were: """

What am I doing wrong here? Can someone help me with this?

Thank you in advance!
Similar Threads
Reputation Points: 23
Solved Threads: 0
Junior Poster in Training
vileoxidation is offline Offline
65 posts
since Feb 2008
Nov 12th, 2009
0
Re: Scheme procedure to capitalize a string
But it is not working. I get an error when I run the procedure, which says this: "string-append: expects type <string> as 2nd argument, given: #<void>; other arguments were: """

What am I doing wrong here?
The issue is that your char_toupper function actually returns void... In fact, the display function (display <>) has no return value. That is, (void? (display "a")) returns #t...

But you are using your char_toupper function as though it were returning an upper case character. If you want to use it like this, then you should modify it so that it actually returns the uppercase character to be used in string-append. Alternatively, you could modify your string_toupper function so that it does not require a result from char_toupper. That is, simply output the uppercase characters one character at a time (as your char_toupper currently does), and do not construct an entirely new string of uppercase characters... This would actually be faster since you would not require string-append, which takes O(n) time (although may not be the purpose of the assignment... it depends on whether or not your string_toupper need return a value...)

Hope that makes sense!
Last edited by n1337; Nov 12th, 2009 at 11:19 am.
Reputation Points: 55
Solved Threads: 10
Junior Poster in Training
n1337 is offline Offline
96 posts
since May 2008
Nov 12th, 2009
0
Re: Scheme procedure to capitalize a string
Click to Expand / Collapse  Quote originally posted by n1337 ...
The issue is that your char_toupper function actually returns void... In fact, the display function (display <>) has no return value. That is, (void? (display "a")) returns #t...

But you are using your char_toupper function as though it were returning an upper case character. If you want to use it like this, then you should modify it so that it actually returns the uppercase character to be used in string-append. Alternatively, you could modify your string_toupper function so that it does not require a result from char_toupper. That is, simply output the uppercase characters one character at a time (as your char_toupper currently does), and do not construct an entirely new string of uppercase characters... This would actually be faster since you would not require string-append, which takes O(n) time (although may not be the purpose of the assignment... it depends on whether or not your string_toupper need return a value...)

Hope that makes sense!
Oh, okay...duh! That makes sense. I hadn't thought about having to change the char_toupper procedure at all, but of course that makes total sense. I do have to use it in the string_toupper procedure, so unfortunately I can't use your idea about just writing the conversion into the string_toupper procedure. Thank you for suggesting it, though!

My procedure now works well, and is ready to go, I think!! Thank you very much for the help, n1337!! I greatly appreciate it.
Reputation Points: 23
Solved Threads: 0
Junior Poster in Training
vileoxidation is offline Offline
65 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: Urgent help with vhdl neeeded
Next Thread in Legacy and Other Languages Forum Timeline: MatLab, find area under experimental data plot





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC