user2113651
user2113651

Reputation: 153

Lower case an entire string in Racket

Is there a way to turn all of the characters in a string into lower case in Racket?

The only way I can think of is turning char-downcase but it won't work with strings

I am using the beginner language so I can't use some functions

Upvotes: 7

Views: 6762

Answers (1)

Óscar López
Óscar López

Reputation: 236140

In practice, you'd use the string-downcase procedure for this:

(string-downcase "ABCDE")
=> "abcde"

But you're working with the beginner's language, so here's the general idea for solving it - I'll give you some hints, it's better if you try to solve the problem by yourself. First, let's split the problem in two parts: one procedure that converts the string into a list of characters, calls a helper procedure that performs the actual transformation and finally turns the converted list back into a string:

(define (lowercase str)
  (<???>           ; convert the list of chars into a string
   (convert        ; call the helper procedure
    (<???> str)))) ; convert the string into a list of chars

The convert procedure is a helper, which does the heavy lifting and converts to lowercase each character in a list of characters:

(define (convert strlst)
  (if <???>                  ; if the list of chars is empty
      <???>                  ; return the empty list
      (cons                  ; else `cons`
       (<???> <???>)         ; convert to lowercase the first char in list
       (convert <???>))))    ; advance recursion over list

The key parts of the solution are the procedures for manipulating strings and characters, click on the links and study the documentation.

Upvotes: 12

Related Questions