[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Swapping characters in a word inside elisp code
From: |
uzibalqa |
Subject: |
Re: Swapping characters in a word inside elisp code |
Date: |
Fri, 28 Jul 2023 19:44:02 +0000 |
Sent with Proton Mail secure email.
------- Original Message -------
On Saturday, July 29th, 2023 at 6:03 AM, uzibalqa <uzibalqa@proton.me> wrote:
> I have a word and want to swap characters at position i with position j.
>
> What would be a good way to do this ? Would I need to change structure
> (to array, vector or some other thing) ?
I have done it this way, but it fails when p > q
How can I solve this ?
(defun cswap (wstr p q)
"Replace characters in WSTR at positions P and Q."
(let ( (char1 (elt wstr p))
(char2 (elt wstr q)) )
(if (and char1 char2)
(concat (substring wstr 0 p)
(char-to-string char2)
(substring wstr (+ p 1) q)
(char-to-string char1)
(substring wstr (+ q 1)))) ))