I have to do a validation for names, I must take the text of a TextBox and verify that no more than 3 letters are repeated consecutively, for example: Aaaaaaro, Maaaaaradiaga, rrrrodriguez, Darwin Perezzzz. These examples should be invalid, however, I could not validate that they were consecutive characters that were counted.
I was trying but the most I managed to do is count how many times each letter is repeated, because it didn't work to know if they were consecutive or not, since I could write "Alcantara" and it would take it as an invalid name since it has 4 times the letter " a" but not consecutively.
public void validar(string cadena)
{
var result = from a in cadena.ToUpper().ToCharArray().Where(Char.IsLetter)
group a by a into g
select new
{
Letra = g.Key,
Repeticiones = g.Count()
};
foreach (var item in result)
{
if (item.Repeticiones >=4 )
{
MessageBox.Show("Una letra se repite mas de 3 veces: " + item.Letra.ToString());
}
else
{
MessageBox.Show("No repiten");
}
}
}
That was the code I used to count the characters, however it doesn't work for what I need. If anyone has an idea I would greatly appreciate it.