Check string rotations

A program to check if strings are rotations of each other or not.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace ConsoleApplication2

{

    class Program

    {

        static void Main(string[] args)

        {

            //Check string rotations

            string s1 = "ABCD";

            //string s2 = "CDAB";

            string s2 = "ACBD";

            if (CheckRotation(s1, s2))

            {

                Console.WriteLine(s2 + " is a rotation of " + s1);

            }

            else

            {

                Console.WriteLine(s2 + " is not a rotation of " + s1);

            }

        }

        public static bool CheckRotation(string str1, string str2)

        {

            if (str1.Length != str2.Length)

            {

                return false;

            }

            else

            {

                str1 = str1 + str1;

                return (CheckSubstring(str1, str2));

            }

        }

        public static bool CheckSubstring(string str1, string str2)

        {

            return (str1.Contains(str2));

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s