C# program to print character at given index in a given string


Following program shows you how to print character at given index in a given string.

using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine("Please enter a string:");
    string input = Console.ReadLine();
    int length = input.Length;
      Console.WriteLine("Please enter an index between 0 to " + (length - 1));
      int index = Convert.ToInt32(Console.ReadLine());
      if (index < 0 || index >= length) {
        Console.WriteLine("Invalid input");
        } else {
          Console.WriteLine("Character at given position is: " + input[index]);
    }
  }
}

Output:

Please enter a string:
 jenni
Please enter an index between 0 to 4
 1
Character at given position is: e