How to check if strings are rotations of each other in C++

1 Answer

0 votes
#include <iostream>

bool rotation(std::string str1, std::string str2) {
    if (str1.length() != str2.length())
        return false;

    std::cout << str1 + str1 << "\n";
   
    return ((str1 + str1).find(str2) != std::string::npos);
}
 
int main()
{
    std::string str1 = "abcxd", str2 = "cxdab";
   
    if (rotation(str1, str2))
        std::cout << "yes" << "\n";
    else
        std::cout << "no" << "\n";
      
    return 0;
}



  
/*
run:
  
abcxdabcxd
yes

*/

 



answered Dec 23, 2021 by avibootz
edited Dec 23, 2021 by avibootz

Related questions

1 answer 112 views
1 answer 175 views
1 answer 123 views
1 answer 191 views
1 answer 97 views
...