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();
}
}
}
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();
}
}
}
本文介绍了一个使用 C# 编写的程序,该程序能够接收用户输入的一段文本,并找出其中最长的连续字母串及其长度。通过遍历字符数组并检查相邻字符是否都为字母来实现这一功能。

5108

被折叠的 条评论
为什么被折叠?



