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

[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: Kaushal Modi
Subject: Re: How to count the number of occurrences of a character in a string?
Date: Tue, 13 Oct 2015 12:31:55 -0400

Sorry, I don't intend to keep on spamming this thread. But I made a
dumb mistake in the previous code.

Below is fixed. The while loop now stops as soon as match-pos is nil;
there was no need to crawl through the whole string.

(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+ (length str))))))
      (message "%0d occurrence%0s of `%c' char found in \"%s\"."
               num-matches (if (/= 1 num-matches) "s" "") char str)
      num-matches))
  (my/count-char-in-string ?a "abcda")
  (my/count-char-in-string ?z "abcda")
  (my/count-char-in-string ?f "falalala")
  (my/count-char-in-string ?l "falalalaabcds")
  (my/count-char-in-string ?^ "f^la^lala^dabra^^")
  (my/count-char-in-string ?\\ "\\falalala\\"))

--
Kaushal Modi


On Tue, Oct 13, 2015 at 12:06 PM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> Thanks Nick, Stefan for the feedback.
>
> Here's the updated code just to fix the issues you pointed out (with a
> little "test-suite" at the end of that progn form) :
>
> (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))
>   (my/count-char-in-string ?a "abcda")
>   (my/count-char-in-string ?z "abcda")
>   (my/count-char-in-string ?f "falalala")
>   (my/count-char-in-string ?^ "f^la^lala^dabra^^")
>   (my/count-char-in-string ?\\ "\\falalala\\"))
>
> --
> Kaushal Modi
>
>
> On Mon, Oct 12, 2015 at 9:18 PM, Stefan Monnier
> <monnier@iro.umontreal.ca> wrote:
>>>           (setq str (substring-no-properties str (1+ match-pos)))
>>
>> This will give wrong results for regexp that use things like ^ and \`.
>>
>> Instead, you should keep using the same `str' and instead pass the
>> `start' arg to string-match.
>>
>>
>>         Stefan
>>
>>



reply via email to

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