dart-exercism/reverse-string/lib/reverse_string.dart
2024-10-14 11:37:17 +02:00

22 lines
505 B
Dart

// String reverse(String word) {
// StringBuffer reversed = StringBuffer();
// for (int i = 0; i < word.length; i++) {
// reversed.write(word[word.length - 1 - i]);
// }
// return reversed.toString();
// }
String reverse(String word) {
List<String> chars = word.split('');
int left = 0;
int right = chars.length - 1;
while (left < right) {
String temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
return chars.join('');
}