GO Program to print ASCII value of each string character

How to print ASCII value of each string character

Here we are explaining how to print the ASCII value of each string character in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.

 

How to print the ASCII value of each string character in the GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing how to print the ASCII value of each string character in the Go language. Here variable str is holding the string need to be converted. And the reader is an object created for reading. Use for loop for printing the ASCII value of each character in the string. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os

STEP 2: Start function main()

STEP 3: Create an object reader for input reading

STEP 4: Read the string into the variable str

STEP 5: Use for loop for printing string characters and its ASCII by using $c, %d

 

GO Source Code

                                          package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)
    fmt.Print("Enter the string to find ASCII = ")
    str, _ := reader.ReadString('\n')
    for i := 0; i < len(str); i++ {
        fmt.Printf("The ASCII value of %c = %d\n", str[i], str[i])
    }
}
                                      

OUTPUT

Enter the string to find ASCII = hello
The ASCII value of h = 104
The ASCII value of e = 101
The ASCII value of l = 108
The ASCII value of l = 108
The ASCII value of o = 111