The
Text
property of a
TextBox
returns a
string
containing the input so far - and a
string
can be treated as an array of characters:
string greeting = "Hello World";
Console.WriteLine($"{greeting[0]} {greeting[6]}");
Will print the first letter of each word:
H W
So to find out how many times a character is repeated at the beginning of a string is simple:
1) Create a variable to hold the "compare character" - call it
firstChar
2) Set
firstChar
to the first character in the string
3) Create a variable to hold the identical values count called
count
, set it to zero.
4) Use a
foreach
loop to look at each character in the string.
4.1) If the character is not the same as
firstChar
, exit the loop using
break
4.2) Otherwise, increment
count
5) After the loop,
count
will contain the number of identical characters at the beginning of the string.