// Challenge: reverse a string (turn the text around).
// Input: "hello"
// Expected output: "olleh"
// Solution 1: using collection methods.
// We split into characters, reverse, and join back together.
String reverse(String text) {
return text.split('').reversed.join('');
}
// Solution 2: iterating from back to front.
// Useful for explaining the "by hand" logic in an interview.
String reverseWithLoop(String text) {
var result = '';
for (var i = text.length - 1; i >= 0; i--) {
result += text[i]; // add each character starting from the end
}
return result;
}
void main() {
print(reverse('hello')); // "olleh"
print(reverse('Flutter')); // "rettulF"
print(reverseWithLoop('world')); // "dlrow"
print(reverseWithLoop('')); // "" (edge case: empty string)
// Explanation:
// split('') separates each character into a list, reversed reverses it,
// and join('') joins it back into a string. The loop version does
// the same manually, iterating from the last index.
}