循环左移字符例如:abcde循环左移2个字符就是cdeab
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 循环左移
{
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("请输入一个字符串:");
string mystring = Convert.ToString(Console.ReadLine());
int length = mystring.Length;
Console.WriteLine("请输入你要左移的位数,不要超过字符串长度"+length);
int N = Convert.ToInt32(Console.ReadLine());
String str=leftshift(mystring, N, length);
Console.WriteLine(str);
Console.WriteLine();
Console.ReadKey();
}
}
}