FastFoodCoding

Login


Connect with one of the following accounts





Login

Please wait while we redirect...

Views: 29   



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

package main

import "fmt"

func main() {

  var input string 
  var length int
  var index int
  fmt.Println("Please enter a string:")
  fmt.Scanf("%s", &input)
  length = len(input)
  fmt.Println("Please enter an index between 0 to " , (length - 1))
  fmt.Scanf("%d", &index)
  if (index < 0 || index >= length) {
    fmt.Println("Invalid input")
    } else {
      fmt.Println("Character at given position is:", string([]rune(input)[index]))
    }
  }

Output:

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



On By


Top Tutorials

Top Questions

Top Articles

Top Blogs

Top News