[英]How do I count how many double letters are in a string?
Double Letters: A letter that appears consecutively twice. 双字母:连续出现两次的字母。 (Ex: "Google", "programming buddy")
(例如:“ Google”,“编程伙伴”)
Google contains 1 double letter. Google包含1个双字母。 Programming buddy contains 2 double letters in the string.
编程伙伴在字符串中包含2个双字母。
Anyways here's my code 反正这是我的代码
int doubleLetters = 0;
for (int i = 0; i < characters.Length - 1; i++)
{
if (characters[i] == characters[i + 1])
{
doubleLetters++;
}
}
The problem here is if a string was spelled "Gooogle" (3 O's), it would return me a count of 2 instead of 1. Basically, it would return me how many conesective letters - 1 as the result. 这里的问题是,如果一个字符串拼写为“ Gooogle”(3 O),它将返回2而不是1。基本上,它将返回我多少个对等字母-结果为1。
How do I make it so it would count the right amount of double letters? 我该如何做才能计算出正确数量的双字母? So if the string is "looooool", it should give a 3. However, my code is counting this as 5 instead.
因此,如果字符串为“ looooool”,则应给出3。但是,我的代码将其计为5。
I'm trying to find code that can fix this. 我正在尝试找到可以解决此问题的代码。
if (characters[i] == characters[i + 1])
{
doubleLetters++;
i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.