unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Kaushal Modi <kaushal.modi@gmail.com>
To: Marcin Borkowski <mbork@mbork.pl>,
	Help Gnu Emacs mailing list <help-gnu-emacs@gnu.org>
Subject: Re: How to count the number of occurrences of a character in a string?
Date: Mon, 12 Oct 2015 21:43:28 +0000	[thread overview]
Message-ID: <CAFyQvY29R-xpAWK6em9fedE1cqVVqRMj6rHsuYWmpp5Yn15feg@mail.gmail.com> (raw)
In-Reply-To: <874mhvq3b2.fsf@mbork.pl>

I took it up as an elisp exercise and got the below (not sure if it is
performance optimal for your application):

The catch form returns the number of matches.

(let ((char ?a)
      (str "abcda"))
  (catch 'break
    (let* ((str str)
           (len (length str))
           (num-matches 0)
           match-pos)
      (dotimes (i len)
        (setq match-pos (string-match-p (char-to-string char) str))
        (when match-pos
          (setq str (substring-no-properties str (1+ match-pos)))
          (setq len (length str))
          (setq num-matches (1+ num-matches)))
        (when (= 0 len)
          (throw 'break num-matches))))))

Below is the same code as above with some debug statements:

(let ((char ?a)
      (str "abcda"))
  (message "%0d occurrences of `%c' char found in \"%s\""
           (catch 'break
             (let* ((str str)
                    (len (length str))
                    (num-matches 0)
                    match-pos)
               (dotimes (i len)
                 (message "i = %0d str = %0s" i str)
                 (setq match-pos (string-match-p (char-to-string char) str))
                 (when match-pos
                   (message "match-pos = %0d str = %0s" match-pos str)
                   (setq str (substring-no-properties str (1+ match-pos)))
                   (message "new str = %0s" str)
                   (setq len (length str))
                   (setq num-matches (1+ num-matches)))
                 (when (= 0 len)
                   (throw 'break num-matches)))))
           char str))





On Mon, Oct 12, 2015 at 3:45 PM Marcin Borkowski <mbork@mbork.pl> wrote:

> Hi all,
>
> the subject line says it all.  Is there any built-in Elisp function to
> do that?  Should I just use cl-count?
>
> Best,
>
> --
> Marcin Borkowski
> http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
> Faculty of Mathematics and Computer Science
> Adam Mickiewicz University
>
>


  reply	other threads:[~2015-10-12 21:43 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-12 19:45 How to count the number of occurrences of a character in a string? Marcin Borkowski
2015-10-12 21:43 ` Kaushal Modi [this message]
2015-10-12 23:27   ` Nick Dokos
2015-10-13  1:18   ` Stefan Monnier
2015-10-13 16:06     ` Kaushal Modi
2015-10-13 16:31       ` Kaushal Modi
2015-10-13 16:48       ` Eli Zaretskii
2015-10-13 17:41         ` Eli Zaretskii
2015-10-13 17:43         ` Kaushal Modi
2015-10-13 19:07           ` Eli Zaretskii
2015-10-13 19:56             ` Kaushal Modi
2015-10-13 20:17               ` Eli Zaretskii
2015-10-13 20:41                 ` Kaushal Modi
     [not found]               ` <mailman.267.1444767491.7904.help-gnu-emacs@gnu.org>
2015-10-13 20:46                 ` Brendan Halpin
2015-10-13 21:07                   ` Kaushal Modi
     [not found]                   ` <mailman.273.1444770543.7904.help-gnu-emacs@gnu.org>
2015-10-13 22:44                     ` Joost Kremers
2015-10-14 10:04                       ` Oleh Krehel
2015-10-14 17:06                         ` Eli Zaretskii
2015-10-13 20:43           ` Michael Heerdegen
2015-10-13 20:59             ` Charles Curley
2015-10-14 18:19           ` Noam Postavsky
2015-10-14 20:00             ` Kaushal Modi
2015-10-13 22:00 ` Emanuel Berg
     [not found] <mailman.164.1444679125.7904.help-gnu-emacs@gnu.org>
2015-10-12 23:35 ` Joost Kremers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFyQvY29R-xpAWK6em9fedE1cqVVqRMj6rHsuYWmpp5Yn15feg@mail.gmail.com \
    --to=kaushal.modi@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=mbork@mbork.pl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).