From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.help Subject: Re: How to count the number of occurrences of a character in a string? Date: Tue, 13 Oct 2015 19:48:15 +0300 Message-ID: <8337xezpdc.fsf@gnu.org> References: <874mhvq3b2.fsf@mbork.pl> NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1444755118 20062 80.91.229.3 (13 Oct 2015 16:51:58 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 13 Oct 2015 16:51:58 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Oct 13 18:51:49 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Zm2nk-0002Ba-95 for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 18:51:48 +0200 Original-Received: from localhost ([::1]:38146 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2nj-0005FR-O4 for geh-help-gnu-emacs@m.gmane.org; Tue, 13 Oct 2015 12:51:47 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48783) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2m7-0004fo-OG for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:50:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Zm2m3-0001pS-Ja for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:50:07 -0400 Original-Received: from mtaout21.012.net.il ([80.179.55.169]:44532) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Zm2m3-0001pE-BC for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 12:50:03 -0400 Original-Received: from conversion-daemon.a-mtaout21.012.net.il by a-mtaout21.012.net.il (HyperSendmail v2007.08) id <0NW6005003UNE200@a-mtaout21.012.net.il> for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 19:48:02 +0300 (IDT) Original-Received: from HOME-C4E4A596F7 ([84.94.185.246]) by a-mtaout21.012.net.il (HyperSendmail v2007.08) with ESMTPA id <0NW60058S401A690@a-mtaout21.012.net.il> for help-gnu-emacs@gnu.org; Tue, 13 Oct 2015 19:48:02 +0300 (IDT) In-reply-to: X-012-Sender: halo1@inter.net.il X-detected-operating-system: by eggs.gnu.org: Solaris 10 X-Received-From: 80.179.55.169 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:107607 Archived-At: > From: Kaushal Modi > Date: Tue, 13 Oct 2015 12:06:36 -0400 > Cc: Help Gnu Emacs mailing list > > (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)