Go language program to find the index of a particular character in a given string

Views: 101   

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

package main

import "fmt"
import "strings"

func main() {

  var input string 
  var character  string
  fmt.Println("Please enter a string:")
  fmt.Scanf("%s", &input)
  fmt.Println("Please enter a character:")
  fmt.Scanf("%s", &character )
  fmt.Println("Index of given character is: " , strings.Index(input,character))
}

Output:

Example1:

Please enter a string:
 hello
Please enter a character:
 e
Index of given character is:  1

Example2:

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