Search code examples
stringdelphipascallazarusfreepascal

Delete short words in a Pascal string


I need to delete short words (shorter that three symbols) in a string ended by a dot. And after to display that string without these words.

So we need a string from a user as an input like this: "This is a simple string a man types." And in this string the program should delete each word that is shorter than three symbols. In our string these are: "is, a, a". So the output should be "This simple string man types."

It's Pascal/Delphi task for my programming course.

I've been thinking about this simple problem for a couple of days and apparently ran out of ideas how to solve it and ask for your help. I tried different loops for the main part which is to basically find sequence of less then three symbols between spaces. I am trying to debug it for quite some time, but to no results.

I use Lazarus 1.6.2, FPC 3.0, and Mac OS X v10.11 (El Capitan). My code is the following:

 VAR
   UserString: string;
   i, n: byte;    // Counters for the symbols in the string. 'i' counts every symbol.
   again: boolean;   // In case we need to repeat the program again
   UserAnswerRepeat: char;
   label repeat_again;

 BEGIN
   again:=false;
 repeat_again:                 // Label to repeat the program
   Writeln('Enter your string (ended by a dot):');
   Readln(UserString);

   // MAIN LOOP:
   n:=1;              // 'n' counts symbols in the words
   for i:=1 to length(UserString) do
   begin
     while (UserString[i] <> ' ') do inc(n); // Here I have Range Check Error. Don't know how to get rid of it.
     if (UserString[n] <> ' ') and (n<3) then
     begin
       delete(UserString, i-n, n);
       n:=1
     end;
   end;

   Writeln('The result is: ', UserString);

   Writeln('Do you want to repeat the program? (Y/N)');
   Readln(UserAnswerRepeat);
   if (UserAnswerRepeat = 'Y') or (UserAnswerRepeat = 'y')  then
     again:=true;
   if (again = true) then
     goto repeat_again;
 END.

Another minor question is: How do you make your console application repeat after the successful run? Here I used labels and goto. Maybe there's another nice way to do it again?


Solution

  • In this situation look for the word separator. In your case it is 'space'. Also, count for the last word before the dot as it is not followed with 'space'.

    One way to write your main loop is:

    var
      LastPos, I: Integer;
      Tmp, UserString: String;
    begin
      LastPos := 1;
      Tmp := '';
      for I := 1 to Length(UserString) do
      begin
        if (UserString[I] = ' ') or (I = Length(UserString)) then
        begin
          if (I - LastPos >= 3) then Tmp := Tmp + Copy(UserString, LastPos, I - LastPos + 1);
            LastPos := I + 1;
        end;
      end;
      UserString: = Tmp; 
    end;