Horizon
Member in Training
Posts: 32
|
Post by Horizon on Jul 4, 2022 21:39:46 GMT
Hello,
I'm trying to stop a program when a space is encountered in a string, I've used the example code below to show my train of thought. Why would the last string not be just 'the' stopping at the space?
print" 'Jumped over' one character at a time." print b$="Jumped over" for i = 1 to len(b$) print left$(b$, i) next i
c$="the lazy fox" for k= 1 to len(c$) d$= left$(c$, k) if d$ = chr$(32) then [stopHere] next k
print [stopHere] print d$ print "Should be just 'the'?" 'Why does this print the whole phrase in c$ not just 'the'
There is the same result if d$ is set as a ' ' (space) value. Any insight would be great.
|
|
|
Post by tsh73 on Jul 4, 2022 21:54:36 GMT
You likely need not whole left part of the string but just single letter
b$="Jumped over" print left$(b$,4) 'Jump print mid$(b$,4, 1) 'p
Also
for k= 1 to len(c$) d$= left$(c$, i) you have loop by 'k' but use inside loop 'i'
It could be made working by checking not whole d$ against space but single rightmost character
c$="the lazy fox" for k= 1 to len(c$) d$= left$(c$, k) 'if d$ = chr$(32) then [stopHere] if right$(d$, 1) = chr$(32) then [stopHere] 'check single rightmost character next k
print [stopHere] print d$
|
|
|
Post by tsh73 on Jul 4, 2022 22:02:25 GMT
Second and third thoughts. 2)I would use MID$ just to spill it letter by letter
c$="the lazy fox" for k= 1 to len(c$) d$= mid$(c$, k, 1) 'single letter print d$; if d$ = chr$(32) then exit for next k
3) current version of JB/LB SERIOUSLY DOESN'T LIKE jumping out of loop this way What harm could ensue? Well, program will mysteriously misbehave and you never get why. (Why? something with keeping order of returns to loop start. Happened with nested loops, FOR loops, WHILE loops, DO loops - any combination)
for k= 1 to len(c$) '... if d$ = chr$(32) then [stopHere] 'don't do that '... next k There is dedicated EXIT FOR operator, use it
for k= 1 to len(c$) '... if d$ = chr$(32) then exit for 'save exit '... next k
4) If you didn't get versed with WORD$() function give it a go. Because it is made just for splitting words and getting first word is as simple as
print word$(c$, 1)
|
|
Horizon
Member in Training
Posts: 32
|
Post by Horizon on Jul 4, 2022 22:11:33 GMT
Thanks Tsh73,
I spotted my typo ('i instead of k') after your post. I see how checking the next right most character for a single space disregards the rest of the string.
I really appreciate the help. Word$ looks very interesting too.
Horizon
|
|
|
Post by jarychk on Jul 4, 2022 23:26:45 GMT
Another possible way is to use INSTR(). I will try to check on this to find how.
LATER ADDING SAMPLE:
Here is something you may use.
[rerun]
print "Give us a string with OR without spaces."
input giveus$
if giveus$="" then end
PRINT
REM look for any leftmost single space within the inputted string.
anyspace=instr(giveus$," ")
if anyspace=0 then
print "program can now continue to rerun because no space was found in your string."
goto [rerun]
end if
REM block in case a single space was found
print "a space was found at position "; anyspace ;" in the string you gave us."
print "This brings program to an END."
END
|
|