- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to reverse a string
Examples
- Input str = “himalaya” => Reverse String would be like => “ayalamih”
- Input str = “mountain” => Reverse String would be like => “niatnuom”
Approach to solve this problem
- Step 1: Define a function that accepts a string, i.e., str.
- Step 2: Convert str to byte string.
- Step 3: Start iterating the byte string.
- Step 4: Swap the first element with the last element of converted byte string.
- Step 5: Convert the byte string to string and return it.
Program
package main import "fmt" func reverseString(str string) string{ byte_str := []rune(str) 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(){ fmt.Println(reverseString("himalaya")) fmt.Println(reverseString("taj")) fmt.Println(reverseString("tropical")) }
Output
ayalamih jat laciport
- Related Articles
- Write a Golang program to reverse a number
- Write a Golang program to reverse an array
- Golang program to reverse a string using stacks
- Write program to reverse a String without using reverse() method in Java?
- Golang program to reverse a slice
- Write a java program to reverse each word in string?
- Write a C program to Reverse a string without using a library function
- Write a program to reverse an array or string in C++
- Golang Program to reverse a given linked list.
- Golang Program to Reverse a Sentence using Recursion
- C# program to reverse a string
- Java Program to Reverse a String
- Write a C program to reverse array
- Write a java program reverse tOGGLE each word in the string?
- Golang Program to demonstrate the example to write double quotes in a string

Advertisements