Sign in to follow this  

PLT Scheme - checking for spaces

This topic is 4276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all I have a question in regards to PLT Scheme, if anyone here actually uses the farking thing. I am trying to count words in a string separated by spaces (the _ symbol for this thread). So "Hello_world" would return 2, as would "Hello_____world". I have no idea where to even start with this, and although I'm not looking for a full answer, if anyone could give me a poke in the right direction it would be much appreciated. For example, how do I even scan from left to right to check what the character is? I'm so lost =(

Share this post


Link to post
Share on other sites
A function that immediately comes to mind: string-ref

i.e:

(string-ref "Hello" 0) will return #\H
(string-ref " Hello" 0) will return #\space

Essentially you could iterate through the input string. While you don't have a space, keep going. When the first space is encountered, increment your # of word counter, and keep iterating until you see something that's not a space. Start over.

I have Dr. Scheme on my laptop. I'm in class right now or I'd play around with it. Long live PLT Scheme! ;)

Share this post


Link to post
Share on other sites
Quote:

For example, how do I even scan from left to right to check what the character is?


Why? I mean, I imagine it's much easier in Scheme to do the opposite, and "dlroW___olleH" will always have the same number of words seperated by spaces...

Share this post


Link to post
Share on other sites
I'm a dork, so I stayed up 26 minutes past my bedtime and wrote the code for you (don't look unless you get stuck): ;)


(define tokenCounter 0)

(define (countStrTokens aStr)
(cond
; current is char and length is 1. increment. done.
((and (not (char=? (string-ref aStr 0) #\space))
(eqv? (string-length aStr) 1))
(set! tokenCounter (+ 1 tokenCounter))
tokenCounter)

; current is space and length is 1. done.
((and (char=? (string-ref aStr 0) #\space)
(eqv? (string-length aStr) 1))
tokenCounter)

; current is char and next is space. increment.
((and (not (char=? (string-ref aStr 0) #\space))
(char=? (string-ref aStr 1) #\space))
(set! tokenCounter (+ 1 tokenCounter))
(countStrTokens (substring aStr 1)))

; current is char and next is char. skip over.
((and (not (char=? (string-ref aStr 0) #\space))
(not (char=? (string-ref aStr 1) #\space)))
(countStrTokens (substring aStr 1)))

; current is space and next is space. skip over.
((and (char=? (string-ref aStr 0) #\space)
(char=? (string-ref aStr 1) #\space))
(countStrTokens (substring aStr 1)))

; current is space and next is char. skip over.
(true (countStrTokens (substring aStr 1)))
)
)

(countStrTokens "hello")(set! tokenCounter 0)
(countStrTokens " hello")(set! tokenCounter 0)
(countStrTokens " hello")(set! tokenCounter 0)
(countStrTokens "hello ")(set! tokenCounter 0)
(countStrTokens "Hello ")(set! tokenCounter 0)
(countStrTokens "Hello there")(set! tokenCounter 0)
(countStrTokens "Hello there ")(set! tokenCounter 0)
(countStrTokens "Hello there ")(set! tokenCounter 0)
(countStrTokens "Hello there dood!")(set! tokenCounter 0)




Produces the output:

Welcome to DrScheme, version 301.
Language: Pretty Big (includes MrEd and Advanced Student).
1
1
1
1
1
2
2
2
3
>

Share this post


Link to post
Share on other sites

This topic is 4276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this