用户从控制台输入一行字符串,程序输出最长的连续字母串的长度和并把它输出。

来源:互联网 发布:网络推广新手 编辑:程序博客网 时间:2024/06/05 21:14
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 输出最长串
{
    class Program
    {
        static void Main(string[] args)    
      {
            int no=0;
            int max=0;
            string  arr =Console.ReadLine();
            char[] ar = new char[arr.Length];
            int i = 0;
            foreach (var a in arr)
            {               
                ar[i] = a;
                i++;              
            }
            int temp = 0;//保留最长字符串的起始位置
            int num = 1;//记录字母数          
            for (int j = 0; j < ar.Length-1; j++)
            {//如果ar[i]和他的下一个都是字符,则将计数加一
                if ((ar[j] >= 'a' && ar[j] <= 'z' || ar[j] <= 'Z' && ar[j] >= 'A')&&
                    (ar[j + 1] <= 'z' && ar[j + 1] >= 'a' || ar[j + 1] <= 'Z'&&ar[j+1]>='A')
                    && (ar[j + 1] >= 'A'))
                { num++;if (num > max){ temp = j + 1; max = num; } }                   
                     else  num = 1;                                           
            }
            Console.WriteLine("最长字符串长度为:" + max );
            Console.Write("它为:");         
            for (int k = temp-max+1; k < temp+1 ; k++)          
                Console.Write( arr[k]+"");
                Console.Read();   
        }
    }
}
阅读全文
0 0
原创粉丝点击