there are two errors that jump out at me right now:
1. you don't check to see if you're at the end of S while in the middle of
inputting each word
2. your repeat loop only gathers 9 strings, but you try to print 10
here's some fixed code.. i tested it with every special case that came to
mind (empty string in S, space or multiple spaces at end of S, etc)
---
Program Tester;
Uses Crt;
Type
Str255 = string[255];
Var
AB:Str255;
Ar:array[1..15] of string;
Procedure getword(S:Str255);
Var
I: Integer;
Output: Str255;
ArN,count:Integer;
eofstr:boolean;
Begin
Output := '';
I := 0; ArN:=1;
eofstr:=FALSE;
if (S<>'') then {don't bother if S if empty}
Repeat
repeat
Inc(I);
if (S[I] <> ' ') then Output := Output + S[I];
if (I = length(S) ) then eofstr:=TRUE;
until (S[I] = ' ') OR eofstr;
if (Output <> '') then begin {output=='' if there is space
at end of string}
Ar[ArN]:=Output;
Output:='';
inc(ArN);
end;
until (ArN=11) or eofstr;
writeln('here is an array of the words entered: ');
if (ArN=1) then writeln('No words were entered.');
for count:= 1 to (ArN-1) do
writeln(Ar[count]);
end; {procedure getword}
Begin
clrscr;
Writeln('start. enter up to 10 words, separated by spaces');
Readln(AB);
Getword(ab);
readln;
end.
---
assas...@reality.halo.nu
In comp.lang.pascal.borland David <da...@fastnet.net.mt> wrote:
: Hello All,
: The Procedure I'm refering to is Getword which is supposed split a sentence
: up into seperate words and put them into an array. The Procedure keeps
: printing a lot of Garbage afterwords and I can't figure out why .. can
: anyone Help??
: Thanking you all,
: David
: Program Tester;
: Uses Crt;
: Type
: Str255 = string[255];
: Var
: AB:Str255;
: Ar:array[1..15] of string;
: Procedure getword(S:Str255);
: Var
: I: Integer;
: Output: Str255;
: ArN:Integer;
: Begin
: Output := '';
: I := 1; ArN:=1;
: Repeat
: Repeat
: Output := Output + S[I];
: Inc(I);
: until (S[I] = ' ');
: inc(I);
: if Output = '' then Output := ' ';
: Ar[Arn]:=Output;
: output:='';
: inc(arn);
: until ((arn=10) or (I+1 = length(S)));
: clrscr;
: for Arn:= 1 to 10 do
: writeln(Ar[arn]);
: end;
: Begin
: Writeln('start');
: Readln(AB);
: Getword(ab);
: readln;
: end.