TransWikia.com

String matching

Code Review Asked by Rajesh Bhat on October 27, 2021

I recently came across a lecture on DFA. I tried to use it to determine whether a given ‘search string’ is found in the ‘input string’. Please tell me if there is a better way.

(display "Enter input string")
(define input-str (read-line (current-input-port)))

(display "Enter Search String")
(define search-str (read-line (current-input-port)))

(define (search-match in-str sr-str)
  (let* ((in-str-len (string-length in-str))
         (start-state 0)
         (end-state (string-length sr-str)))
   (letrec ((iter (lambda (i curr-state)
                    (cond ((= curr-state end-state) #t)
                          ((= i in-str-len) #f)
                          ((equal? (string-ref in-str i) 
                                  (string-ref sr-str curr-state))
                           (iter (+ i 1) (+ curr-state 1)))
                          (else 
                           (iter (+ i 1) start-state))))))
     (iter 0 0))))

(search-match input-str search-str)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP