首页 > 编程 > C# > 正文

c#循环左移字符示例

2020-01-24 02:42:45
字体:
来源:转载
供稿:网友

循环左移字符例如: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();
        }
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表