OK.I changed the code to this and worked.
That's OK, But your code has 2 problems.
first you should not name you variable after a pascal-basetype like string and integer.
I put your code in a test-Function
function TestCode(aText: string;MaxSpace:integer=3): string;
// I assumed these variables are initialized here
const
Space = ' ';
var i:integer=0;
spacecount:integer=0;
begin
repeat
if aText[i]=space then
Inc(spacecount);
Inc(i);
if (spacecount<MaxSpace)and(i<=Length(aText)) then begin
result:=aText;
end
else if (spacecount=MaxSpace)and (i<=Length(aText)) then
begin
result:=Copy(aText,1,i);
Exit;
end;
until (i>Length(aText));
end;
And test it against:
writeln(TestCode('In the Name of God'));
writeln(TestCode('In the Name of God, maker of all'));
writeln(TestCode('In my Name'));
writeln(TestCode('In my Name ')+'<');
writeln(TestCode('In my Name ')+'<');
writeln(TestCode('In my Name',0)); // you'd expect the whole test but you only get 'I'
writeln(TestCode('In my Name'));
writeln(TestCode('')+'<'); // here you get a SIGSEGV
I assumed your string-variable is a string, then you get a Segmentation Fault (SIGSEGV)
if you use shortstring instead you don't get that fault but the line 2 output 'In the N' instead of 'In the Name o'
Better do a length-check before "aText[ i ]"
if length(aText) = 0 then
begin
result := aText;
exit; // see next comment about exit.
end;
or
if (length(aText) >0) and (aText[i]=space) then
You should use exit with care.
In your case, if you only want to go to the end of the repeat-until construct, use break instead of exit
so if you want to execute code after until, it's not affected by the string.
Have you thought of other Whitespaces like <tab> ?