11965 - Extra Spaces

All about problems in Volume CXIX. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Re: 11965 - Extra Spaces

Postby brianfry713 » Fri Apr 27, 2012 9:08 pm

Not in freepascal. Use ansistring instead of string.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11965 - Extra Spaces

Postby Hasselli » Sun Apr 29, 2012 7:20 pm

Code: Select all
procedure Shift(var s: ansistring; ind: Integer);
var
  i: Integer;

begin
  for i := ind to length(s) do
    s[i] := s[i + 1];
  setlength(s, length(s) - 1);
end;

var
  i, j, t, tt, n: Integer;
  s: ansistring;

begin
  Readln(t);
  for tt := 1 to t do
  begin
    Readln(n);
    Writeln('Case ', tt, ':');
    for i := 1 to n do
    begin
      Readln(s);
      for j := 1 to Length(s) do
        While (s[j] = ' ') and (s[j + 1] = ' ') do
          Shift(s, j);
      Writeln(s);
    end;
    if tt < t then
   Writeln;
  end;
end.

got Runtime Error with this code.
Hasselli
New poster
 
Posts: 22
Joined: Mon Apr 16, 2012 8:08 pm

Re: 11965 - Extra Spaces

Postby brianfry713 » Mon Apr 30, 2012 9:32 pm

You are trying to read beyond the length of the string, if j=length(s), s[j+1] would give RE. Also your algorithm is too slow.
brianfry713
Guru
 
Posts: 1765
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Previous

Return to Volume CXIX

Who is online

Users browsing this forum: No registered users and 0 guests