|
Hi! My first post here at techtalk (finally!). Actually I have this program for my exam syllabus and I've got the program here but it's not functioning as I want it to.
What the program does is calculate the no of words in a sentence and display that and then it re-arranges those words in alphabetical order.
I got the code from my teacher (during notes), but it was as usual riddled with mistakes. I was able to rectify it quite a bit and now am able to make it calculate the number of words correctly; however, the problem crops up while re-arranging words alphabetically.
Please note that the logic used from 120 has just been copied by me from the notes. . . and that is where the problem lies. So, could you please go through the code and point out where I'm going wrong here?
10 READ A$ 20 PRINT A$ 25 DATA "every wednesday we have computer practicals. " 30 L=LEN(A$) 40 B$=" ":C=1: REM I've defined an empty string as B$. 50 FOR I=1 TO L 60 W$=MID$(A$,I,1) 70 IF W$<>" " THEN B$=B$+W$:GOTO 100: REM What this line does is group characters together to form words. So, when a blank space is detected, it skips the THEN part. 80 C=C+1: REM Simultaneously calculating number of words 90 X$(C)=B$:B$" ": REM Re-initializing B$ to blank string to find another word 100 NEXT I 110 PRINT "Number of words in the sentence are: ",C 120 FOR I=1 TO C-1 130 FOR J=1 TO C 140 IF X$(I)<=X$(J) THEN 180 150 T$=X$(I) 160 X$(I)=X$(J) 170 X$(J)=T$:REM 150 to 170 are for creation of a temp string to switch strings in case there are bigger or smaller. 180 NEXT J 190 NEXT I 200 FOR I=1 TO C+2 210 PRINT X$(I);" "; 220 NEXT I 230 END
When I run it I get the o/p:
computer every have we wednesday
As you can see, it skipped displaying the last word 'practicals'. I tried re-arranging the words in the program itself at line no 25 and every time it skipped arranging the last word. . . What am I doing wrong here that the last word is not being taken into computation?
ThanQ very much for going through my problem! :shiny
|