Home > Software design >  delete all words with more than 4 letters in a string (Pascal)?
delete all words with more than 4 letters in a string (Pascal)?

Time:12-05

I was assigned a task for university where I have to write a program which deletes all words with more than 4 letters. I really have no clue at all. I would be very thankful for any kind of help.

VAR
UserString: string; //должна быть строка на 40 символов и точку в конце
i, n: byte;

BEGIN
Writeln('Enter the string:');
Readln(UserString);

i:=0;
n:=1;    

repeat       //MAIN LOOP:

  inc(i);
  if (UserString[i] = ' ') or (UserString[i] = '.') then

     begin
        if (i-n<3)then
        begin
        delete(UserString, n, i-n 1);
        i:=n-1;
        end;
        n:=i 1
     end

until (UserString[i] = '.') or (i>length(UserString));

Writeln('Result String: ', UserString);
END.

I tried this. and its working on onlinegdb but not on Delphi... and I don't know why...

CodePudding user response:

You should break up the logic into smaller utility functions for each task you need (finding a word, getting the word's length, deleting the word and any subsequent whitespace, etc). It will make the code easier to read and maintain.

For example:

function FindNextWordStart(const S: string; var Index: Integer): Boolean;
var
  Len: Integer;
begin
  Len := Length(S);
  while (Index <= Len) and (Ord(S[Index]) <= 32) do Inc(Index);
  Result := (Index <= Len);
end;

function GetWordLength(const S: string; Index: Integer): Integer;
var
  StartIdx, Len: Integer;
begin
  Len := Length(S);
  StartIdx := Index;
  while (Index <= Len) and (Ord(S[Index]) > 32) do Inc(Index);
  Result := (Index - StartIdx);
end;

procedure DeleteWord(var S: String; Index, WordLen: Integer);
var
  StartIdx, Len: Integer;
begin
  Len := Length(S);
  StartIdx := Index;
  Inc(Index, WordLen);
  while (Index <= Len) and (Ord(S[Index]) <= 32) do Inc(Index);
  Delete(S, StartIdx, Index - StartIdx);
end;

var
  UserString: string;
  StartIdx, WordLen: Integer;
begin
  Writeln('Enter the string:');
  Readln(UserString);

  StartIdx := 1;
  while FindNextWordStart(UserString, StartIdx) do
  begin
    WordLen := GetWordLength(UserString, StartIdx);
    if WordLen > 4 then
      DeleteWord(UserString, StartIdx, WordLen)
    else
      Inc(StartIdx, WordLen);
  end;

  Writeln('Result String: ', UserString);
end.

Online Demo

CodePudding user response:

I guess you can solve your task with TStringlist class:

var  AStrLst : TStringlist ;
     i : Integer ,
begin
    AStrLst := TStringlist.Create ;
    try
       //  use  this char for separation  of words 
       AStrLst.Delimiter :=' ';
       AStrLst.DelimitedText := ' here comes my sample string ';

       for I := AStrLst.Count-1 to 0 do
          begin
          //  delete item from list  if ...
          if length( trim(AStrLst[i])) <= 4  then AStrLst.Delete(i);
          end;
    finally
       //    get  the complete
       writeln ( AStrLst.Text ) ;
       AStrLst.Free;
    end;
end; 

I did not test this code - but hope it helps - to get this code running, your home work

  • Related