Write function that checks if a given string (case insensitive) is a palindrome.
import "package:test/test.dart"; import "package:solution/solution.dart";
void main() { group("Fixed tests", () { test("Testing for 'a'", () => expect(isPalindrome("a"), equals(true))); test("Testing for 'aba'", () => expect(isPalindrome("aba"), equals(true))); test("Testing for 'Abba'", () => expect(isPalindrome("Abba"), equals(true))); test("Testing for 'hello'", () => expect(isPalindrome("hello"), equals(false))); test("Testing for 'Bob'", () => expect(isPalindrome("Bob"), equals(true))); test("Testing for 'Madam'", () => expect(isPalindrome("Madam"), equals(true))); test("Testing for 'AbBa'", () => expect(isPalindrome("AbBa"), equals(true))); test("Testing for ''", () => expect(isPalindrome(""), equals(true)));
내답:
bool isPalindrome(String x) {
String text = x.toLowerCase();
String reverse = text.split('').reversed.join('');
if(text == reverse)
{
return true;
}else{
return false;
}
}
bool isPalindrome(String x) {
x = x.toLowerCase();
for (int i = 0; i < x.length ~/ 2; i++) {
if (x[i] != x[x.length - 1 - i]) return false;
}
return true;
}
bool isPalindrome(String x) {
return x.toUpperCase().split("").reversed.join() == x.toUpperCase();
}