Sobes.tech
Junior

How to determine if a string is a palindrome in Dart?

sobes.tech AI

Answer from AI

bool isPalindrome(String input) {
  // Remove spaces and convert the string to lowercase for correct comparison.
  final cleanedInput = input.replaceAll(' ', '').toLowerCase();

  // Get the reversed string.
  final reversedInput = cleanedInput.split('').reversed.join('');

  // Compare the original string without spaces/case with its reverse.
  return cleanedInput == reversedInput;
}

// Example usage
// void main() {
//   print(isPalindrome("A man a plan a canal Panama")); // true
//   print(isPalindrome("hello")); // false
// }
How to determine if a string is a palindrome in Dart… - sobes.tech