Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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)
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.