Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Problem No 7

Reverse a String with a Loop

Easy

6 minute session

Summary

Learn to reverse the characters in a string using iteration.

Problem statement

Given a string s, return a new string that contains the characters of s in reverse order. Use iteration (for loop or while loop) to traverse the string and build the reversed result. Do not use built-in slicing reversal (e.g., s[::-1]) in the starter exercise; learners should practice loop-based construction.

Task

Write a function that returns the input string with characters in reverse order.

Examples

Simple example

Input

reverse_string("hello")

Output

olleh

Explanation

The input 'hello' reversed is 'olleh'.

Input format

A single function call reverse_string(s) where s is a string.

Output format

Return a string containing the characters of s in reverse order.

Constraints

0 <= len(s) <= 1000. The string may include spaces, punctuation, and Unicode characters.

Samples

Sample input 0

reverse_string("abcde")

Sample output 0

edcba

Explanation 0

Each character is placed in reverse order.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

05:11 PM

Free preview includes 1 Teach Theory response and 1 AI hint per unlocked preview lesson.