2012-11-10 4 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) 

이렇게하면 각 문자가 대문자로 변환되어 표시됩니다. 그러나 나는 오류가 계속해서 찾아 낼 수 있습니다. 도움이 필요해. 감사합니다라켓 - 상단 케이스

+1

계속 오류가 있습니까? –

+0

문자열 길이 : 계약 위반 예상 됨 : 문자열? – user1815262

+0

읽기가 반드시 문자열을 반환하지는 않습니다. 다시 값을 반환하지만 그 값은 사용자가 입력 한 내용에 따라 문자열, 심볼 또는 숫자, 목록 또는 ...이 될 수 있습니다. 따라서 http 대신 http : // http : //docs.racket-lang.org/reference/Byte_and_String_Input.html# (def._ ((quote._ ~ 23 ~ 25kernel) ._read-line)) – dyoo

답변

2

라켓에서는 if 대신에 when을 사용해야합니다.

즉, 다음 표현식의 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