Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit StrUtils;
- interface
- function GetWordsCount(Str: string): integer;
- function GetWordById(Str: string; Id: integer): string;
- function DeleteWordById(Str: string; Id: integer): string;
- implementation
- function GetWordsCount(Str: string): integer;
- const
- letters : set of char =
- ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
- var
- found: boolean;
- i: integer;
- begin
- Result := 0;
- i := 1;
- repeat
- found := false;
- while (i <= Length(Str))and(not(Str[i] in letters)) do
- inc(i);
- while (i <= Length(Str))and(Str[i] in letters) do begin
- found := true;
- inc(i);
- end;
- if found then
- inc(Result);
- until (i >= Length(Str))or(not found);
- end;
- function GetWordById(Str: string; Id: integer): string;
- const
- letters : set of char =
- ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
- var
- i, k: integer;
- begin
- Result := '';
- i := 1;
- k := 1;
- repeat
- Result := '';
- while (i <= Length(Str))and(not(Str[i] in letters)) do
- inc(i);
- while (i <= Length(Str))and(Str[i] in letters) do begin
- if (k = Id) then
- Result := Result+Str[i];
- inc(i);
- end;
- inc(k);
- until (i >= Length(Str))or(k = Id+1);
- end;
- function DeleteWordById(Str: string; Id: integer): string;
- const
- letters : set of char =
- ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
- var
- i, k, n: integer;
- begin
- Result := '';
- i := 1;
- k := 1;
- repeat
- Result := '';
- n := 0;
- while (i <= Length(Str))and(not(Str[i] in letters)) do
- inc(i);
- while (i <= Length(Str))and(Str[i] in letters) do begin
- if (k = Id) then
- inc(n);
- inc(i);
- end;
- if n <> 0 then
- Delete(Str, i-n, n);
- inc(k);
- until (i >= Length(Str))or(k = Id+1);
- Result := Str;
- end;
- end.
- {=========== example ===========}
- //writeln(GetWordsCount('one two three'));
- //writeln(GetWordById('one two three', 3));
- //writeln(DeleteWordById('one two three', 2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement