Golang Strings | Find Output Programs | Set 1

This section contains the Golang strings find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on September 04, 2021

Program 1:

package main

import "fmt"

func main() {
	var str string = "www.includehelp.com"
	fmt.Printf("%s\n", str)
	fmt.Printf("%c\n", str[4])
}

Output:

www.includehelp.com
i

Explanation:

In the above program, we created a string str initialized with "www.includehelp.com". Then we printed string str and character at index 4.


Program 2:

package main

import "fmt"

func main() {
	var str string = "www.includehelp.com"
	fmt.Printf("%d\n", str[4])
}

Output:

105

Explanation:

In the above program, we created a string str initialized with "www.includehelp.com". Then we printed the ASCII value of character 'i', which is stored at index 4 in the string str.


Program 3:

package main

import "fmt"

func main() {
	var str string = "www.includehelp.com"
	fmt.Printf("%d\n", len(str[4]))
}

Output:

./prog.go:7:24: invalid argument str[4] (type byte) for len

Explanation:

The above program will generate a syntax error because we cannot use the len() function for the specific character.


Program 4:

package main

import "fmt"

func main() {
	fmt.Printf("%d\n", len("www.includehelp.com"))
}

Output:

19

Explanation:

In the above program, we printed the length of the string "www.includehelp.com" on the console screen.


Program 5:

package main

import "fmt"

func main() {
	fmt.Println("M"[0])
}

Output:

77

Explanation:

In the above program, we printed the ASCII value of "M" on the screen.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.