How can I print every second character in a string? Example:

Hello, my name is Jack! 

There must be something like this (maybe a mistake): el,maeiak

  • 2
    As usual: go through a cycle of letters and type every second character. - Enikeyshchik

1 answer 1

I wondered if the string was iterable in Go and I wrote the following code:

 package main import "fmt" func main() { src := "Hello, my name is Jack!" for i, ch := range src { if i % 2 == 0 { fmt.Printf("%c", ch) } } } 

I got the following conclusion:

 Hlo ynm sJc! 
  • one
    It is displayed starting from the first character (index 0). To output from the second, you need to check for i% 2 == 1. - Enikeyshchik
  • Thank you, what I need! :) - you have no pass
  • @ Enikeyschik and thank you for this amendment :) - you have no pass