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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值