Left Rotation of String

Left rotation of a string is to move some leading characters to its tail. For e.g. if the input string is “AamolDGote” and a number 2, the rotated result is “molDGoteAa”.

 private static void LeftRotateString(string input, int numberOfCharacters)
{
#region Approach 1
string strFirst = input.Substring(0, numberOfCharacters);
string strSecond = input.Substring(numberOfCharacters, input.Length - numberOfCharacters);
string result = string.Concat(strSecond, strFirst);
Console.WriteLine(result);
#endregion

#region Approach2
string str = input;
str = str + input; //Append same string 
result = str.Substring(numberOfCharacters, input.Length - 1);
Console.WriteLine(result);
#endregion
}
Advertisements

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