Q:

Golang Strings | Find Output Programs | Set 2

0

This section contains the Golang strings find output programs (set 2) with their output and explanations.

Program 1:

package main

import "fmt"
import "strings"

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

Program 2:

package main

import "fmt"

func main() {
	var str string = "ABC XYZ"

	if str[3] == 0x20 {
		fmt.Println("Hello")
	} else {
		fmt.Println("Hiiii")
	}
}

Program 3:

package main  
import "fmt"  
import "strings"  

func main() {  
    var strArr = []string{"h","e","l","l","o"}  
    var ch     = "*";
    var str    = "";
    
    str = strings.Join(strArr,ch);
    
    fmt.Println(str);  
}  

Program 4:

package main  

import "fmt"  
import "strings"  

func main() {  
    var strArr = []string{"ABC","GHI","LMN","PQR","XYZ"}  
    var ch     = "-->";
    var str    = "";
    
    str = strings.Join(strArr,ch);
    
    fmt.Println(str);  
}  

Program 5:

package main

import "fmt"
import "strings"

func main() {
	var str = "hello "
	fmt.Println(str.Repeat(5))
}

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer Program 1:

Output:

73

Explanation:

In the above program, we created a string variable str initialized with "www.includehelp.com". Then we converted the created string into uppercase and got the character from index 4 that 'I'. After that, we printed the ASCII value of 'I' that is 73 on the screen.


 

Answer Program 2:

Output:

Hello

Explanation:

In the above program, we created a string str initialized with "ABC XYZ". Then we checked string str contains space at index 3 and print the appropriate message on the screen.

Note: The hexadecimal value of space is 0x20.


 

Answer Program 3:

Output:

h*e*l*l*o

Explanation:

In the above program, we created an array string strArr. Then we joined strings with a specified string using Join() function. After that, we printed the result on the screen.


 

Answer Program 4:

Output:

ABC-->GHI-->LMN-->PQR-->XYZ

Explanation:

In the above program, we created an array string strArr. Then we joined strings with the specified string "-->" using Join() function. After that, we printed the result on the screen.


 

Answer Program 5:

Output:

./prog.go:4:8: imported and not used: "strings"
./prog.go:8:17: str.Repeat undefined (type string has no field or method Repeat)

Explanation:

The above program will generate a syntax error because we did not correctly use the Repeat() function.

The correct program is given below,

package main

import "fmt"
import "strings"

func main() {
	var str = "hello "
	fmt.Println(strings.Repeat(str, 5))
}

// Output: hello hello hello hello hello

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now