About Me

My photo
Mostly software programming related blogs.

Friday, October 26, 2012

String rotation


/* Program to check given two strings s1 and s2, if one is rotation of
 * other one. For example student is rotation of dentstu.
 */
#include <iostream>
#include <string.h>

using namespace std;

main() {
        string s1, s2;
   
        cout << "Enter both the strings ";

        cin >> s1;
        cin >> s2;

        string temp = s1+s1;

        if (temp.find(s2) == string::npos) {
                cout << s1 << " and " << s2 << " are not rotation of each other." << endl;
        } else {
                cout << s1 << " and " << s2 << " are rotation of each other." << endl;
        }  

}

$ ./a.out 
Enter both the strings student dentstu
student and dentstu are rotation of each other.

$ ./a.out 
Enter both the strings abc acb
abc and acb are not rotation of each other.

No comments: