Advertisement
deced

Untitled

Nov 3rd, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 1.71 KB | None | 0 0
  1. program Lab3_1;
  2.  
  3. uses System.SysUtils, System.StrUtils;
  4.  
  5. Const
  6.     Separators = ' ,.:;-?!()[]{}@#$%&';
  7.  
  8. var
  9.     Input: String;
  10.  
  11. function GetEndIndex(str: String; I: Integer): Integer;
  12. begin
  13.     while not(ContainsText(Separators, str[I])) do
  14.     begin
  15.         inc(I)
  16.     end;
  17.     dec(I);
  18.     GetEndIndex := I;
  19. end;
  20.  
  21. function GetNextWordIndex(str: String; I: Integer): Integer;
  22. begin
  23.     inc(I);
  24.     while (ContainsText(Separators, str[I])) do
  25.     begin
  26.         inc(I)
  27.     end;
  28.     GetNextWordIndex := I;
  29. end;
  30.  
  31. function GetWord(str: String; fromIndex, ToIndex: Integer): String;
  32. var
  33.     I: Integer;
  34.     ret: string;
  35. begin
  36.     ret := '';
  37.     if ToIndex > high(str) then
  38.         ToIndex := high(str);
  39.     for I := fromIndex to ToIndex do
  40.         ret := ret + str[I];
  41.     GetWord := ret;
  42. end;
  43.  
  44. function Swap(str: String): String;
  45. var
  46.     I, firstEndIndex, secondStartIndex, secondEndIndex: Integer;
  47.     ret, Word1, Word2, Splitter, temp: String;
  48. begin
  49.     I := 1;
  50.     while I < Length(str)+1 do
  51.     begin
  52.         if ContainsText(Separators, str[I]) then
  53.         begin
  54.             ret := ret + str[I];
  55.         end
  56.         else
  57.         begin
  58.             firstEndIndex := GetEndIndex(str, I);
  59.             secondStartIndex := GetNextWordIndex(str, firstEndIndex);
  60.             secondEndIndex := GetEndIndex(str, secondStartIndex);
  61.             Word1 := GetWord(str, I, firstEndIndex);
  62.             Word2 := GetWord(str, secondStartIndex, secondEndIndex);
  63.             Splitter := GetWord(str, firstEndIndex + 1, secondStartIndex - 1);
  64.             I := secondEndIndex;
  65.             ret := ret + Word2 + Splitter + Word1;
  66.         end;
  67.        inc(I);
  68.     end;
  69.     Swap := ret;
  70. end;
  71.  
  72. begin
  73.  
  74. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement