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)
       {
           char char1 = Convert.ToChar(Console.Read());
           char char2 = Convert.ToChar(Console.Read());

           if(char.IsLetter(char1) && (char.IsLetter(char2)))
           Console.Write(char1);
           Console.Write(char2);
       }
   }
}

Can anyone please help me to solve it?

Hi Sam,

There are many things that are not in place for this task to be solved correctly. In short:
first, read an integer count from the console.
Then in a loop read count chars from the console input. for loop will fit well for this. When reading a char, check if it is a letter, and in case it is - print it to the screen.

2 Likes

I tried to read every letter but I can’t

 for(int n = 0; n <numberOfNumber; n++)
           {
               if(char.IsLetter(char1[0]))
               Console.Write(char1);

Ok, this is a start!
I think you are missing reading from the console inside the loop. First, read the char:

char char1 = Convert.ToChar(Console.Read());

Then perform the check with IsLetter, as you did. Please try it out and let me know if it worked.

1 Like

i try like this :
but it doesn’t work (due to [ ]), I don’t know where to position them so that it finally works.

        int numberOfNumber = int.Parse(Console.ReadLine());

        char char1 = Convert.ToChar(Console.Read());

        for(int n = 0; n <numberOfNumber; n++)

        {

            if(char.IsLetter(char1[0]))

            Console.Write(char1);

        }

You are very close! You need to read the char1 inside the loop, not before it:


for(int n = 0; n <numberOfNumber; n++)
{
    char char1 = Convert.ToChar(Console.Read());
...

As char1 is a single char, not an array, you should remove the [0] and treat char1 as a char variable.
After you apply these changes I think it is going to pass all tests!

1 Like

Whow, thank you very much, it was not so difficult after all
thank you for your instructions! :smile: :grin:

1 Like