[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to count the number of occurrences of a character in a string?
From: |
Eli Zaretskii |
Subject: |
Re: How to count the number of occurrences of a character in a string? |
Date: |
Tue, 13 Oct 2015 19:48:15 +0300 |
> From: Kaushal Modi <kaushal.modi@gmail.com>
> Date: Tue, 13 Oct 2015 12:06:36 -0400
> Cc: Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
>
> (progn
> (defun my/count-char-in-string (char str)
> "Count the number of times CHAR character appears in STR string."
> (message "\n==========\nstr = %0s" str)
> (let* ((num-matches 0)
> (ptr 0) ; initiate pointer for string match
> match-pos)
> (while (<= ptr (length str))
> (message "ptr = %0d" ptr)
> (setq match-pos (string-match-p
> (regexp-quote (char-to-string char)) str ptr))
> (if match-pos
> (progn
> (setq ptr (1+ match-pos))
> (message "match-pos = %0d ptr = %0d" match-pos ptr)
> (setq num-matches (1+ num-matches)))
> (progn
> (setq ptr (1+ ptr)))))
> (message "%0d occurrence%0s of `%c' char found in \"%s\"."
> num-matches (if (/= 1 num-matches) "s" "") char str)
> num-matches))
Isn't the following simpler?
(let ((str-list (append str nil))
(num-matches 0))
(while str-list
(if (= (car str-list) char)
(setq num-matches (1+ num-matches)))
(setq str-list (cdr str-list)))
num-matches)
- How to count the number of occurrences of a character in a string?, Marcin Borkowski, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Nick Dokos, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Stefan Monnier, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?,
Eli Zaretskii <=
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Message not available
- Re: How to count the number of occurrences of a character in a string?, Brendan Halpin, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Message not available
- Re: How to count the number of occurrences of a character in a string?, Joost Kremers, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Oleh Krehel, 2015/10/14