C# program to find the index of a particular character in a given string

Views: 148   

Following program shows you how to find the index of a particular character in a given string.

using System;

class MainClass {
  public static void Main (string[] args) {
    Console.WriteLine("Please enter a string:");
    string input = Console.ReadLine();
    Console.WriteLine("Please enter a character:");
    string character = Console.ReadLine(); 
    Console.WriteLine("Index of given character is: " + input.IndexOf(character));
  }
}

Output:

Example1:

Please enter a string:
 abcdefg
Please enter a character:
 d
Index of given character is: 3

Example2:

Please enter a string:
 abcdefg
Please enter a character:
 k
Index of given character is: -1
On By