[英]Check for repeated character in a string
Check if the input string contains at least one letter that appears twice in a row, like example:检查输入字符串是否包含至少一个连续出现两次的字母,例如:
xx, XX,
abcdde (dd), abcdde (dd),
aabbccdd (aa, bb, cc, or dd), aabbccdd(aa、bb、cc 或 dd),
ugknbfddgicrmopn (double dd). ugknbfddgicrmopn(双 dd)。
The below method doesn't work下面的方法不起作用
input.Distinct().Count();
As it return true for awea
(repeated a).因为它为awea
返回 true(重复 a)。 I only have to check for repeated character continuously.我只需要不断检查重复的字符。
This will return true if your string contains any multiple chars:如果您的字符串包含任何多个字符,这将返回 true:
input.Distinct().Count() != input.Length;
You need to check the current and next character index of the input string.您需要检查输入字符串的当前和下一个字符索引。 Please see sample code below.请参阅下面的示例代码。
static void Main(string[] args)
{
var input = Console.ReadLine();
var result = HasRepeatedCharacters(input);
Console.WriteLine(result);
Console.ReadLine();
}
public static bool HasRepeatedCharacters(string input)
{
bool hasRepeatedCharacters = false;
if(input.Length >= 2)
{
for (var index = 0; index < input.Length - 1; index++)
{
if(input[index] == input[index + 1])
{
hasRepeatedCharacters = true;
}
}
}
return hasRepeatedCharacters;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.