數組既然已經瞭解了基本知識/ C#編程語言的基礎,我現在正試圖解決我的第一個實際問題:寫一個程序,給定一個字符串,認定其包含至少一個大寫字母但沒有數字最長子串(並隨後顯示該最長子串的長度)。這可能是兩個對於可接受的密碼預選賽條件,例如...檢查兩個條件同時遍歷字符
我已經寫了下面的所有代碼由我自己,這意味着可能存在性能問題,但這是以後考慮。我被困在我必須確保子字符串中沒有數字的地方。在我的代碼中的註釋說明了我的思維,而寫程序...
我想首先我應該檢查,看看是否有在抽取的子串的大寫字母,如果有,那麼我就可以將該合格的子字符串存儲在列表中,然後跳出循環。但是現在我想知道如何在同一個子字符串中同時檢查無數字狀態?
我試圖保持它整潔和簡單(正如我所說,我剛剛開始編寫超過幾行的程序!),所以我想做一個嵌套循環檢查每個字符!char.IsNumber(letter)
可能不是最佳。或者我應該先檢查一下是否沒有數字,然後看看是否至少有一個大寫字母?
我感到困惑如何實現這兩個限制,所以我希望在解決這個問題提供一些幫助。我也希望任何意見或你可能有建議。例如,是否可以將我的子字符串存儲在列表中?我應該編一本什麼字典嗎?我所有可能的子字符串提取嵌套循環是最優的嗎?
附:有些位仍未完成;例如我仍然執行最後一步找到最長的子字符串,並顯示給用戶的長度...
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";
}
}
}
}
}
你可以把所有的號碼的索引字符串中,減去隨後指數會給你時間最長的數字少串,然後就尋找那些大寫字母。你有一個循環3點,這可能永遠不會是最佳的。 – Bmo