2
\$\begingroup\$

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)
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.