0

I have to write a program that changes a string's vowels, consonants and other symbols into C, V respectively 0. I've done this but I wonder if there is a more efficient and elegant way to do it. Would appreciate input.

(defun string-to-list (string)
(loop for char across string collect char))

(defun is-vowel (char) (find char "aeiou" :test #'char-equal))

(defun is-consonant (char) (find char "bcdfghjklmnpqrstvwxyz" :test #'char-equal))

(defun letter-type (char)
(if (is-vowel char) "V"
(if (is-consonant char) "C"
"0")))

(defun analyze-word (word-string)
(loop for char across word-string collect (letter-type char)))

Moreover, I would like to make it a string, how could I do that? Should I define a function that would iterate through the list and make it a string or is it an easier way to do it?

5

2 Answers 2

2
(defun letter-type (char)
  (cond ((find char "aeiou" :test #'char-equal) #\V)
        ((alpha-char-p char) #\C)
        (t #\0)))

CL-USER> (map 'string #'letter-type "analyze-word")
"VCVCCCV0CVCC"
Sign up to request clarification or add additional context in comments.

2 Comments

Though when I type in "spectacuular22ace" it gives me "VCVCCCV0CVCC" and spectacular starts with a v which is a consonant not a vowel and more than that, it only prints one 0 and not two 0.
You have to replace the "analyze-word" in the above example with your input.
0

Just for the sake of the idea:

(defun multi-replace-if (sequence function &rest more-functions)
  (map (type-of sequence)
       (lambda (x)
         (loop for f in (cons function more-functions)
            for result = (funcall f x)
            while (eql x result)
            finally (return result)))
       sequence))

(multi-replace-if "bcdfghjklmnpqrstvwxyz"
                  (lambda (x) (if (find x "aeiouy") #\v x))
                  (lambda (y) (declare (ignore y)) #\c))
"cccccccccccccccccccvc"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.