📖 Scenario: Imagine you have a list of words that you want to reverse to see them backwards. This is like reading a word from right to left instead of left to right.
🎯 Goal: You will create a program that reverses a given string using a simple approach. You will first store the string, then prepare a helper variable, then reverse the string, and finally print the reversed string.
📋 What You'll Learn
Create a string variable with a specific value
Create a helper variable to hold the reversed string
Use a for loop to reverse the string character by character
Print the reversed string
💡 Why This Matters
🌍 Real World
Reversing strings is useful in text processing, palindromes checking, and coding puzzles.
💼 Career
Understanding string manipulation is fundamental for software development, data processing, and algorithm design.
Progress0 / 4 steps
1
DATA SETUP: Create the original string
Create a string variable called original and set it to the value "hello".
DSA Go
Hint
Use original := "hello" to create the string variable.
2
CONFIGURATION: Create a helper variable for the reversed string
Create a string variable called reversed and set it to an empty string "".
DSA Go
Hint
Use reversed := "" to create an empty string variable.
3
CORE LOGIC: Reverse the string using a for loop
Use a for loop with variable i starting from len(original) - 1 down to 0. Inside the loop, add the character at original[i] to reversed.
DSA Go
Hint
Use a for loop counting down from the last index to zero and add each character to reversed.
4
OUTPUT: Print the reversed string
Use fmt.Println(reversed) to print the reversed string. Remember to import "fmt" at the top.
DSA Go
Hint
Use fmt.Println(reversed) to show the reversed string on the screen.
Practice
(1/5)
1. What does the strings.Reverse function do in Go if it existed? (Assume it reverses the string.)
easy
A. It converts the string to uppercase.
B. It returns the input string with characters in reverse order.
C. It removes all vowels from the string.
D. It splits the string into words.
Solution
Step 1: Understand the function name and purpose
The function name Reverse suggests it changes the order of characters.
Step 2: Interpret the expected behavior
Reversing a string means the last character becomes first, and so on.
Final Answer:
It returns the input string with characters in reverse order. -> Option B
Quick Check:
Reverse means flip order [OK]
Hint: Reverse means flip the order of characters [OK]
Common Mistakes:
Confusing reverse with uppercase conversion
Thinking reverse removes characters
Assuming reverse splits words
2. Which of the following Go code snippets correctly reverses a string s?
easy
A. var reversed string
for i := len(s)-1; i >= 0; i-- {
reversed += string(s[i])
}
B. for i := 0; i < len(s); i++ {
reversed += string(s[i])
}
C. reversed := strings.ToUpper(s)
D. reversed := strings.Split(s, " ")
Solution
Step 1: Analyze the loop direction
var reversed string
for i := len(s)-1; i >= 0; i-- {
reversed += string(s[i])
} loops from the last character to the first, building reversed string.
Step 2: Check other options
for i := 0; i < len(s); i++ {
reversed += string(s[i])
} loops forward, so no reversal. reversed := strings.ToUpper(s) changes case, reversed := strings.Split(s, " ") splits words.
Final Answer:
Option A correctly reverses the string. -> Option A
Quick Check:
Loop backward to reverse [OK]
Hint: Loop backward from end to start to reverse [OK]
Common Mistakes:
Looping forward instead of backward
Using string functions that don't reverse
Confusing splitting with reversing
3. What is the output of this Go code?
func main() {
s := "hello"
reversed := ""
for i := len(s) - 1; i >= 0; i-- {
reversed += string(s[i])
}
fmt.Println(reversed)
}
medium
A. olleh
B. hello
C. h
D. error
Solution
Step 1: Trace the loop from end to start
The loop starts at index 4 ('o') and goes to 0 ('h'), appending characters in reverse order.