PLT Scheme - checking for spaces
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 =(
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! ;)
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! ;)
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...
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): ;)
Produces the output:
(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).111112223>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement