Example of c looping left shift characters

  • 2020-06-19 11:37:23
  • OfStack

For example: abcde a loop that moves 2 characters to the left is cdeab


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace  Cyclic shift to the left 
{
    class Program
    {
         static string reverse(  char[] char2, int i, int j)
        {
            for (int begin=i,end=j;begin < end; begin++, end--)
            {
                char temp = char2[begin];
                char2[begin] = char2[end];
                char2[end] = temp;
            }
            return new String(char2);
        }
        static string leftshift( string str,int i ,int j)
        {
            char[] char1 = str.ToCharArray();
            reverse( char1,0,i-1);
            reverse( char1,i,j-1);
            reverse( char1, 0, j - 1);
            return new String(char1);
        }
        static void Main(string[] args)
        {
            Console.WriteLine(" Please enter the 1 String: ");
            string mystring = Convert.ToString(Console.ReadLine());
            int length = mystring.Length;
            Console.WriteLine(" Please enter the number of digits you want to move to the left, not more than the length of the string "+length);
            int N = Convert.ToInt32(Console.ReadLine());
            String str=leftshift(mystring, N, length);
            Console.WriteLine(str);
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}


Related articles: