help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [External] : Swapping characters in a word inside elisp code


From: Drew Adams
Subject: RE: [External] : Swapping characters in a word inside elisp code
Date: Fri, 28 Jul 2023 20:32:09 +0000

> 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) ?

(Homework?)

Depends what you mean by swap chars in a word.
And whether your word is represented by a string,
a vector, a list...  And how you want the result:
in a separate string, vector,... or in the same
one, modified.

(Again, you don't make clear what you want.)

Here's one way to swap chars in a string
destructively:

(defun cswap (string p q)
  "Swap chars in STRING at positions P and Q.
This is a destructive operation."
  (aset string p (prog1 (aref string q)
                        (aset string q (aref string p))))
  string)

(setq s1 "123456789")
(cswap s1 3 5) ; s1 = "123654789"
               ;          ^ ^

`prog1' is often used to swap things.
A more typical use is swapping values
of two variables:

(setq start (prog1 end (setq end start)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]