fork download
  1. ; titlecase
  2.  
  3. (define (char-alphanumeric? c)
  4. (or (char-alphabetic? c) (char-numeric? c)))
  5.  
  6. (define (titlecase str)
  7. (let ((str (string-downcase str)) (inword #f))
  8. (do ((i 0 (+ i 1))) ((= i (string-length str)) str)
  9. (if inword
  10. (when (char-whitespace? (string-ref str i))
  11. (set! inword #f))
  12. (when (and (char-alphanumeric? (string-ref str i))
  13. (or (zero? i)
  14. (not (char-alphanumeric? (string-ref str (- i 1))))))
  15. (set! inword #t)
  16. (string-set! str i (char-upcase (string-ref str i))))))))
  17.  
  18. (display (titlecase "programming PRAXIS")) (newline)
  19. (display (titlecase "\"prax'is\"")) (newline)
  20. (display (titlecase "123p")) (newline)
Success #stdin #stdout 0.03s 8656KB
stdin
Standard input is empty
stdout
Programming Praxis
"Prax'is"
123p