Golang Program to Find the Reverse of a String


May 5, 2022, Learn eTutorial
1531

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

What is the reverse of a string?

In this program, we are reversing the string using GO. Reversing the string is the process of displaying the user input string in reverse order. For example, if the user entered string is PROGRAMMER then the output of the program will be REMMARGORP.
How to find the reverse of a string in GO?

It is quite simple and there are many ways to find the reverse of a string using GO. But in this program, we are finding the reverse of the string by swapping the letters, like first with last and second with second last and so on.

Here use the built-in function fmt.println() to print anything on the output screen and fmt.scanln() for reading the input values. These functions are 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.

In this program first read the input string from the user by using built-in functions fmt.println()  and fmt.scanln(). Save the input string into the variable s. Then call the function reverseString(s) that will define a function that accepts the input string s. In this function, first, convert string s to a byte string. We can find the length of the byte string using the built-in function len(). Start iterating the byte string. In each iteration swap the first element with the last element of the converted byte string. Finally, convert the byte string into the string and return it to the main function.

By performing the below steps we can find the reverse of the string.

ALGORITHM

STEP 1: Import the package fmt
STEP 2: Open the main() to start the program, GO program execution starts with the main()
STEP 3: Declare the variables s as a string
STEP 4: Read the input string from the user into s
STEP 5: Call the function reverseString(s)
STEP 6: Display the reverse string as output

Steps to implement reverseString(s)

STEP 1: Define a function that accepts our input string, i.e., s.
STEP 2: Convert s to a byte string.
STEP 3: Start iterating the byte string by using for loop.
STEP 4: In each iteration swap the first element with the last element of the converted byte string.
STEP 5: Convert the byte string to string and return it to the main program.
 

Golang Source Code

                                          package main
import "fmt"
func reverseString(s string) string{
   byte_str := []rune(s)
   for i, j := 0, len(byte_str)-1; i < j; i, j = i+1, j-1 {
      byte_str[i], byte_str[j] = byte_str[j], byte_str[i]
   }
   return string(byte_str)
}

func main(){
var s string
fmt.Println ("Enter the string")
fmt.Scan(&s)
fmt.Println ("The reverse string of",s,"is")
fmt.Println(reverseString(s))
}
                                      

OUTPUT

Enter the string
programmer
The reverse string of programmer is
remmargorp