gpt4 book ai didi

c# - 在遍历字符数组时检查两个条件

转载 作者:行者123 更新时间:2023-12-03 19:39:22 29 4
gpt4 key购买 nike

在学习了C#编程语言的基础知识/基础知识之后,我现在正在尝试解决我的第一个实际问题:写一个程序,给定一个字符串,找到其最长的子字符串,该子字符串包含至少一个大写字母,但是没有数字(然后显示此最长子字符串的长度)。对于可接受的密码,这可能是两个限定条件,例如...

我自己编写了所有代码,这意味着可能存在性能问题,但这供以后考虑。我被困在必须确保子字符串中没有数字的地方。我的代码中的注释显示了我在编写程序时的想法...

我认为首先应该检查提取的子字符串中是否存在大写字母,如果存在,则可以将该合格的子字符串存储在列表中,然后退出循环。但是,现在我想知道如何在同一子字符串中同时检查无数字的情况吗?

我试图使其保持整洁和简单(正如我所说的,我刚开始编写程序的时间超过了几行!),所以我认为做一个嵌套循环来检查每个字符是否与!char.IsNumber(letter)可能不是最佳选择。还是应该先检查是否没有数字,然后再检查是否至少有一个大写字母?

我对如何实现这两个限制感到困惑,因此希望能为解决此问题提供帮助。如果您有任何意见或建议,我也将不胜感激。例如,可以将我的子字符串存储在列表中吗?我应该制作某种字典吗?我的所有可能子字符串提取嵌套循环是否最优?

ps。有些位还没有完成。例如,我仍要执行最后一步,以找到最长的子字符串并向用户显示其长度...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PasswordRestriction
{
class Program /// Write a program that, given a string, finds the longest substring that is a valid password and returns its length.
{
static void Main(string[] args)
{
// Ask the user for an alphanumeric string.
Console.WriteLine("Enter a string of alphanumeric values:");

// Receive the user input as string.
string password = Console.ReadLine();

// Print the length of the longest qualifying substring of the user string.
Console.WriteLine("Length of the longest qualifying substring:\n" + Solution(password).Length );

// Prevent the console window from closing.
Console.ReadLine();
}


/// The method that exracts the longest substring that is a valid password.
/// Note that a substring is a 'contiguous' segment of a string.
public static string Solution(string str)
{
// Only allow non-empty strings.
if ( String.IsNullOrEmpty(str) )
{
return "";
}
else
{
// Only allow letters and digits.
if ( str.All(char.IsLetterOrDigit) )
{
// A list for containing qualifying substrings.
List<string> passwordList = new List<string>();

// Generate all possible substrings. Note that
// string itself is not a substring of itself!
for (int i = 1; i < str.Length; i++)
{
for (int j = 0; j <= (str.Length-i); j++)
{
string subStr = str.Substring(j, i);
Console.WriteLine(subStr);

bool containsNum = false;
bool containsUpper = false;

// Convert the substrings to arrays of characters with ToCharArray.
// This method is called on a string and returns a new character array.
// You can manipulate this array in-place, which you cannot do with a string.
char[] subStrArray = subStr.ToCharArray();

// Go through every character in each substring.
// If there is at least one uppercase letter and
// no digits, put the qualifying substring in a list.
for (int k = 0; k < subStrArray.Length; k++)
{
char letter = subStrArray[k];

if ( char.IsNumber(letter) )
{
containsNum = true;
break;
}

if ( char.IsUpper(letter) )
{
containsUpper = true;
}

if ( containsUpper && (containsNum == false) && (k == subStrArray.Length - 1) )
{
Console.WriteLine("Found the above legit password!");
passwordList.Add(subStr);
}
}
}
}

//Find the longest stored string in the list.
//if (passwordList.Count != 0)
//{
string maxLength = passwordList[0];

foreach (string s in passwordList)
{
if (s.Length > maxLength.Length)
{
maxLength = s;
}
}
//}

// Return the qualifying substring.
return maxLength;
}
else
{
return "aaaaaaaaaa";
}
}
}
}
}

最佳答案

Linq的一个好问题


不含数字-数字上的Split
至少一个大写字母-Where + Any
最长(不是最短)OrderByDescending
最长(仅一个)-FirstOrDefault


实作

string source = ....

var result = source
.Split('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
.Where(line => line.Any(c => c >= 'A' && c <= 'Z')) // or char.IsUpper(c)
.OrderByDescending(line => line.Length)
.FirstOrDefault(); // null if there're no such substrings at all

关于c# - 在遍历字符数组时检查两个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38445104/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com