How to check if a string is a rotation of another string in Pascal

1 Answer

0 votes
program RotationCheck;

uses SysUtils;

function IsRotation(s1, s2: string): Boolean;
begin
  IsRotation := (Length(s1) = Length(s2)) and (Pos(s2, s1 + s1) <> 0);
end;

var
  s1, s2: string;
begin
  s1 := 'abbc';
  s2 := 'cabb';
  if IsRotation(s1, s2) then
    WriteLn('yes')
  else
    WriteLn('no');
    
  s1 := 'abbc';
  s2 := 'bbac';
  if IsRotation(s1, s2) then
    WriteLn('yes')
  else
    WriteLn('no');
end.

      
       
(*
run:
    
yes
no
   
*)

 



answered Feb 3 by avibootz
...