我的程序需要检查字符串中的3个连续字母(并检查整个字符串)。我可以让它以一种笨拙的方式检查它们,例如"检查qwe","检查" wer",检查" ert",但这看起来很乱,做得很糟。
static void Main(string[] args)
{
string BadLetters = "qwertyuiopasdfghjklzxcvbnm";
string password = "Blablauio";
for (int i = 1; i <= 30; i++)
{
// This checks if it contains "qwe" but i want it to
// cycle through the rest (such as "wer" or "rty")
if (password.Contains(BadLetters.Substring(0, 3))) {
Console.WriteLine("password contains 3 consequtive letters in BadLetters");
}
}
Console.ReadKey();
}
问题是这只会检查BadLetters(qwe)的前3个字母,而且它不会查找&#34; ert&#34;等。
答案 0 :(得分:1)
如果循环密码变量会更好,如下所示:
string badLetters = "qwertyuiopasdfghjklzxcvbnm";
string password = "Blablauio";
for (int i = 0; i < password.Length-2; i++)
{
if (badLetters.Contains(password.Substring(i,3)))
{
Console.WriteLine("password contains 3 consequtive letters in BadLetters");
}
}
显然,您还必须检查密码是否至少为3个字符 这个循环可能在键盘行交叉字母上失败,即&#34; opa&#34;或者&#34; pas&#34;,这应该被认为是正确的值,所以你可以这样做:
string badLettersR1 = "qwertyuiop";
string badLettersR2 = "asdfghjkl";
string badLettersR3 = "zxcvbnm";
string password = "Blablauio";
for (int i = 0; i < password.Length-2; i++)
{
if (badLettersR1.Contains(password.Substring(i,3)) ||
badLettersR2.Contains(password.Substring(i,3)) ||
badLettersR3.Contains(password.Substring(i,3)))
{
Console.WriteLine("password contains 3 consequtive letters in BadLetters");
}
}
答案 1 :(得分:0)
也许你可以尝试编辑你的字符串,所以3个字母都消失了,然后你就像以前一样做下一个字母。
或者您可以添加2个变量firstletter
和secondletter
4,因此它会跳过前3个变量并重复以下变量
我知道你的字符串是连续3个......
答案 2 :(得分:0)
您需要遍历password
中的每个字符,在BadLetters
中找到其索引,并检查password
中的后两个字符是否与BadLetters
中的后两个字符匹配。我还改变了for loop
的停止条件,因为你只需要遍历password
中的倒数第二个字符
string BadLetters = "qwertyuiopasdfghjklzxcvbnm";
string password = "Blablauio";
for (int i = 0; i < password.Length - 2; i++)
{
var j = BadLetters.IndexOf(password[i]);
if (j > -1 && j + 2 < BadLetters.Length &&
password[i + 1] == BadLetters[j + 1] &&
password[i + 2] == BadLetters[j + 2])
{
Console.WriteLine("password contains 3 consequtive letters in BadLetters");
}
}
答案 3 :(得分:0)
您可以浏览符号数组并比较它的索引,如下所示:
if (BadLetters.IndexOf(my_word[i]) - BadLetter.IndexOf(my_word[i-1]) == 1) {
Console.WriteLine("Consequent letters detected!");
}
您可以计算后续字母,并在计数超过 3
时提醒我提供了键盘中所有行的详细代码。你可以添加另一行(即大写)而无需修改任何代码。
您还可以控制N
- 字符串中禁止的后续字符数。
还有Check
方法仅用于演示工作结果:
q - ok
qw - ok
qwe - password contains 3 consequtive letters in BadLetters
abdfsk - ok
ehjk - password contains 3 consequtive letters in BadLetters
bnm - password contains 3 consequtive letters in BadLetters
.net上的代码小提琴:https://dotnetfiddle.net/4oILkj
public static String[] KeyboardLines = new [] {
"1234567890",
"qwertyuiop[]",
"asdfghjkl;'\\",
"`zxcvbnm,./"
};
public static Int32 GetLine(char c){
for (int i = 0; i < KeyboardLines.Length; i++) {
if (KeyboardLines[i].IndexOf(c) > -1) {
return i;
};
}
return -1;
}
public static bool HasConsequenceLetters(string str, int n = 3) {
if (str.Length < n) {
return false;
}
char previousLetter = str[0];
int previousLine = GetLine(previousLetter);
int previousLetterIndex = KeyboardLines[previousLine].IndexOf(previousLetter);
Int32 consequentLettersCount = 1;
for (int i = 1; i < str.Length; i++) {
var currentLetter = str[i];
var currentLine = GetLine(currentLetter);
var currentLetterIndex = KeyboardLines[currentLine].IndexOf(currentLetter);
if (currentLine != -1 && currentLine == previousLine) {
if (currentLetterIndex - previousLetterIndex == 1) {
consequentLettersCount += 1;
}
}
else {
consequentLettersCount = 1;
}
if (consequentLettersCount == n) {
return true;
}
previousLetter = currentLetter;
previousLetterIndex = currentLetterIndex;
previousLine = currentLine;
}
return false;
}
如果GetLine
函数将返回带行号的字符索引,然后不仅比较字符而是比对(LetterLine,LetterIndex),则可以改进此方法。但这要求我们使用元组或类,但我不认为你真的想要这个。
答案 4 :(得分:0)
我发现的一个简单的是
using System;
public class Program
{
public static void Main()
{
string BadLetters = "qwertyuiopasdfghjklzxcvbnm";
string password = "Blablauio";
Console.WriteLine(password.IndexOf(BadLetters.Substring(0,3))>=0?"Present":"Not Present");
Console.ReadLine();
}
}
您可能需要检查两个字符串的空状态