Search code examples
c#stringif-statement

How to check repeated letters in a string c#


I am creating a program that checks repeated letters in a string.

For Example:

wooooooooooow
happpppppppy

This is my code:

 string repeatedWord = "woooooooow";
 for (int i = 0; i < repeatedWord.Count(); i++)
 {
     if (repeatedWord[i] == repeatedWord[i+1])
     {
          // ....
     }
 }

The code works but it will always have an error because the last character [i + 1] is empty/null.

The error is Index was outside the bounds of the array.

Any solution for this?


Solution

  • run the loop until repeatedWord.Count()-1