Advertisement
Guest User

StrUtils

a guest
Dec 12th, 2011
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.00 KB | None | 0 0
  1. unit StrUtils;
  2.  
  3. interface
  4.  
  5. function GetWordsCount(Str: string): integer;
  6. function GetWordById(Str: string; Id: integer): string;
  7. function DeleteWordById(Str: string; Id: integer): string;
  8.  
  9. implementation
  10.  
  11. function GetWordsCount(Str: string): integer;
  12. const
  13.   letters : set of char =
  14.    ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
  15. var
  16.   found: boolean;
  17.   i: integer;
  18. begin
  19.   Result := 0;
  20.   i := 1;
  21.   repeat
  22.     found := false;
  23.     while (i <= Length(Str))and(not(Str[i] in letters)) do
  24.       inc(i);
  25.     while (i <= Length(Str))and(Str[i] in letters) do begin
  26.       found := true;
  27.       inc(i);
  28.     end;
  29.     if found then
  30.       inc(Result);
  31.   until (i >= Length(Str))or(not found);
  32. end;
  33.  
  34. function GetWordById(Str: string; Id: integer): string;
  35. const
  36.   letters : set of char =
  37.    ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
  38. var
  39.   i, k: integer;
  40. begin
  41.   Result := '';
  42.   i := 1;
  43.   k := 1;
  44.   repeat
  45.     Result := '';
  46.     while (i <= Length(Str))and(not(Str[i] in letters)) do
  47.       inc(i);
  48.     while (i <= Length(Str))and(Str[i] in letters) do begin
  49.       if (k = Id) then
  50.         Result := Result+Str[i];
  51.       inc(i);
  52.     end;
  53.     inc(k);
  54.   until (i >= Length(Str))or(k = Id+1);
  55. end;
  56.  
  57. function DeleteWordById(Str: string; Id: integer): string;
  58. const
  59.   letters : set of char =
  60.    ['A'..'z', 'a'..'z', 'А'..'Я', 'а'..'я', 'Ё', 'ё'];
  61. var
  62.   i, k, n: integer;
  63. begin
  64.   Result := '';
  65.   i := 1;
  66.   k := 1;
  67.   repeat
  68.     Result := '';
  69.     n      := 0;
  70.     while (i <= Length(Str))and(not(Str[i] in letters)) do
  71.       inc(i);
  72.     while (i <= Length(Str))and(Str[i] in letters) do begin
  73.       if (k = Id) then
  74.         inc(n);
  75.       inc(i);
  76.     end;
  77.     if n <> 0 then
  78.       Delete(Str, i-n, n);
  79.     inc(k);
  80.   until (i >= Length(Str))or(k = Id+1);
  81.   Result := Str;
  82. end;
  83.  
  84. end.
  85.  
  86.  
  87. {=========== example ===========}
  88. //writeln(GetWordsCount('one two three'));
  89. //writeln(GetWordById('one two three', 3));
  90. //writeln(DeleteWordById('one two three', 2));
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement