-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.el
55 lines (44 loc) · 1.54 KB
/
13.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
;; Using a ‘while’ loop, write a function to count the number of
;; punctuation marks in a region—period, comma, semicolon, colon,
;; exclamation mark, and question mark. Do the same using recursion.
;; Using while loop
(defun count-punctuation (beg end)
"Count number of punctualtion in the 'region'
between 'beg' and 'end'"
(interactive "r")
(message "Counting punctuations...")
(save-excursion
(goto-char beg)
(let ((count 0))
(while (and (< (point) end)
(re-search-forward "[.,;:!?]" end t))
(setq count (1+ count)))
(cond ((zerop count)
(message "Region has no punctuation"))
(t
(message "Region has %d punctuation(s)" count))))))
;; Usage
;; try selecting below region to see 'count-punctuation' working
"This s, a. sample text! OMG!! with: punctuatioin?? ;"
;; Using recursion
(defun count-punctuation-recursive (beg end)
"Count number of punctuation in the 'region'
between 'beg' and 'end'"
(interactive "r")
(message "Counting punctuation")
(save-excursion
(goto-char beg)
(let ((count (recursive-count end)))
(cond ((zerop count)
(message "Region has no punctuation"))
(t
(message "Region has %d punctuation(s)" count))))))
(defun recursive-count (end)
"Actual recursive fuction that counts punctuation"
(if (and (< (point) end)
(re-search-forward "[.,;:?!]" end t))
(1+ (recursive-count end))
0))
;; Usage
;; try selecting below region to see 'count-punctuation-recursive' working
"This s, a. sample text! OMG!! with: punctuatioin?? ;"