Please help me with the task: "Output character number"

Hello, I’m working on this task:

Write a program that reads a string and an integer number N from the console. It should then output the string’s _N_th character to the console. For example:
>Make deals with machines
>4
e

Here is my code:


namespace CharType
{
   class OutputCharacterNumber
   {
       static void Main(string[] args)
       {
           string sentence = Console.ReadLine();
           int N = int.Parse(Console.ReadLine());
           Console.WriteLine(sentence[N]);
       }
   }
}

Can anyone please help me to solve it?
Works fine with any other text, but somehow “Make deals with machines” doesn’t output anything as it is counting from 0 unlike the task which starts from 1. Is this seriously not my fault?

The trick here is that humans start indexing from 1, and computers - start with 0. So if you receive the string ABC and you, as a human being asked “What is the 2nd character?” the answer is B. The programs we write are made for humans to use, so the expected behavior is that indexing starts at 1.
For our example, with ABC your program will output: sentence[2] which is C. But it should output B. Do you have an idea how to modify the code so that it outputs the ‘B’?

I have no idea how to do that. My code outputs empty space that is between “Make” and “deals”

Ok, here is the hint: you need to subtract 1 from the index. N-1 instead of N.

thanks, just figured it out

1 Like

:+1: :confetti_ball: :tada: