Please help me with the task: Only letters output

Hello, I’m working on this task:

Write a program that reads an integer count, for the number of characters to read, from the console. Then, from a new line, the program should read count characters written in one line from the console. Finally, the program should output only the letters from the characters to the console on one line. For example:
>10
>FL45H0b12c
FLHbc

Here is my code:


namespace CharType
{
   class OnlyLetters
   {
       static void Main(string[] args)
       {
           int count = int.Parse(Console.ReadLine());

           for (int i = 0; i < count; i++)
           {
               char firstChar = Convert.ToChar(Console.Read());
               if (char.IsLetter(firstChar))
               {
                   Console.Write(firstChar);
               }


               char secondChar = Convert.ToChar(Console.Read());
               if (char.IsLetter(secondChar))
               {
                   Console.Write(secondChar);
               }
           }
       }
   }
}

Can anyone please help me to solve it?

I am getting a System.OverflowException: value was either too large or too small for a character error.
Works in Visual Studio, however.

Hi @petrien ,

Welcome to CodeEasy forum!

In your solution, you have a for loop, reading count characters from the console, but you read 2 characters in each loop iteration - thus, you try to read 2*count characters. Just remove the
char secondChar = ...
part in the for loop, and the task will be solved :+1:

1 Like

Thank you very much!

1 Like