2012-11-10 149 views
0
; 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) 

該轉換每個字符爲大寫,並進行顯示。但是我不斷收到錯誤,我可以找到它。任何幫助。謝謝球拍-UpperCase

+1

你不斷收到錯誤是什麼? –

+0

字符串長度:違反合同 預期:string? – user1815262

+0

讀取不一定返回字符串。它返回一個值,但該值可能是一個字符串,一個符號,一個數字或一個列表,或者......取決於用戶輸入的內容。因此,您可能想使用read-line代替:http ://docs.racket-lang.org/reference/Byte_and_String_Input.html#(def._((quote._〜23〜25kernel)._read-line)) – dyoo

回答

2

在球拍中,您必須編寫when而不是if在單臂的情況下。

即,在下面的表達式更改ifwhen

(if (> x 96) 
    ; then display the character equivalent to the ASCII value given by y 
    (display (integer->char y))) 

另請注意,string-upcase是內置的。

+1

同樣, char-upcase也內置於:http://docs.racket-lang.org/reference/characters.html#(def._((quote._~23~25kernel)._char-upcase)) – dyoo