:do (rotatef (aref string i)
-(defun reverse-words (string)
- "Reverse the words in the string STRING in place."
- (labels ((reverse-next-word (word-start)
- (if (>= word-start len)
- (let ((word-end (or (position #\Space string :start word-start
- (reverse-substring! string word-start word-end)
- (reverse-next-word (1+ word-end))))))
- (reverse-next-word 0)))
+(defun reverse-words! (string)
+ "Reverse the individual words in the string STRING in place."
+ (let ((len (length string))
+ (dotimes (i (1+ len) string)
+ (when (or (= len i) (char= #\Space (aref string i)))
+ (reverse-substring! string start i)
+ (setf start (1+ i))))))
;; Note that 0 bytes were allocated during the REVERSE-WORDS
;; CL-USER> (let ((s (copy-seq "Hello my 1 name is quad")))
-;; (time (reverse-words s)))
+;; (time (reverse-words s)))
;; Timing the evaluation of (REVERSE-WORDS S)
;; "olleH ym 1 eman si dauq"