The program that counts each letters in a given text. Counting program the total letter in text in c#.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void Main(string[] args) { string myString = Console.ReadLine(); int count = 0; for (int i = 0; i < myString.Length; i++) { // check the char for whitespace. If char is not whitespace, increase the count variable if (!char.IsWhiteSpace(myString[i])) { count++; } } Console.WriteLine("Total letters: "+ count); Console.ReadLine(); } |
Alternavite code (foreach loop):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | static void Main(string[] args) { Console.Write("Enter string :"); string myString = Console.ReadLine(); int count = 0; foreach (char item in myString) { // check the char for whitespace. If char is not whitespace, increase the count variable if (!char.IsWhiteSpace(item)) { count++; } } Console.WriteLine("Total letters: "+ count); Console.ReadLine(); } |
Output: