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

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

RE: count char in string [The Emacs Challenge Competition - round 2, wi


From: Drew Adams
Subject: RE: count char in string [The Emacs Challenge Competition - round 2, with fallout]
Date: Thu, 29 Oct 2020 09:13:55 -0700 (PDT)

> cl-count? seq-count? seq-reduce? KISS!
> 
> (defun count-char-in-string (char str)
>   "Return number of occurrences of character CHAR in string STR."
>   (let ((count 0))
>     (dotimes (i (length str) count)
>       (when (eq char (aref str i))
>       (setq count (1+ count))))))

+1.

Or just (cl-incf count) at the end, if you're
loading `cl-lib.el' anyway, for other reasons.



reply via email to

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