Golang program to iterate through each character of string


A string in Golang is a collection of characters. Since strings in Go are immutable, they cannot be modified after they have been produced. Concatenating or adding to an existing string, however, enables the creation of new strings. A built-in type in Go, the string type can be used in a variety of ways much like any other data type.

Syntax

func len(v Type) int

The len() function is used to get the length of any parameter. It takes one parameter as the data type variable whose length we wish to find and returns the integer value which is the length of the variable.

func Split(str, sep string) []string

Split() function is used to split a string through a provided separator. This function is present in strings package and it accepts the string to split as an argument along with a separator. The function then returns the final array of strings as a result.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package) package

  • Step 2 − Create a function main and in that function create a string of which each character is iterated.

  • Step 3 − Using the user-defined or internal function to iterate through each character of string

  • Step 4 − The print statement is executed using fmt.Println() function where ln means new line.

Example 1

In this example, we will see how to use for loop to iterate through each character of string. The output will be the character printed on console along with a space character.

package main
import "fmt"

func main() {
	mystr := "Hello, alexa!" //create string
	fmt.Println("The original string given here is:", mystr)
	fmt.Println("The iteration performed through each character of string:")
	for i := 0; i < len(mystr); i++ {   //run a loop and iterate through each character
		fmt.Printf("%c ", mystr[i])  //print characters along with the space character
	}
}

Output

The original string given here is: Hello, alexa!
The iteration performed through each character of string:
H e l l o ,   a l e x a ! 

Example 2

In this example we will see how to iterate through each character of string using rune() method where str is converted into slice of runes which is further iterated and printed on the console.

package main
import "fmt"
func main() {
	mystr := "Hello, alexa!"  //create string
	fmt.Println("The original string given here is:", mystr)
	runes := []rune(mystr)  //turn string to slice 
	fmt.Println("The iteration performed through each character of string:")

	for _, v := range runes { //iterate through rune
		fmt.Printf("%c ", v)  //print characters along with space character
	}
}

Output

The original string given here is: Hello, alexa!
The iteration performed through each character of string:
H e l l o ,   a l e x a ! 

Example 3

In this example, we will get to know how to iterate through each character of string using strings.Split() function where it splits the string into slice and iterates through each character.

package main
import (
	"fmt"
	"strings"
)

func main() {
	mystr := "Hello, alexa!"   //create string
	fmt.Println("The original string given here is:", mystr)
	fmt.Println("The iteration performed through each character of string:")
	for _, slice := range strings.Split(mystr, "") {  //use built-in function to split the string
		fmt.Printf("%s ", slice)  //print the characters along with space character
	}
}

Output

The original string given here is: Hello, alexa!
The iteration performed through each character of string:
H e l l o ,   a l e x a !

Conclusion

We executed the program of iterating through each character of string using three examples. In the first example we used for loop, in the second example we used rune() built-in function and in the third example we used strings.Split() function.

Updated on: 01-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements