From 9b0a9e95d52c580c1a2c2259f8651b476d79bc83 Mon Sep 17 00:00:00 2001 From: Satyam Shukla <61821492+satyamnoob@users.noreply.github.com> Date: Fri, 7 Oct 2022 18:42:49 +0530 Subject: [PATCH 1/2] Create palindrome.dart --- strings/palindrome.dart | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 strings/palindrome.dart diff --git a/strings/palindrome.dart b/strings/palindrome.dart new file mode 100644 index 00000000..837046f5 --- /dev/null +++ b/strings/palindrome.dart @@ -0,0 +1,16 @@ +void main(){ +print('Enter Words or number'); + // User enter a string or a number + String? original = 'hannah'; + + // then we will reverse the input + String? reverse = original.split('').reversed.join(''); + // then we will compare + if(original == reverse) + { + print('Its A Palindrome'); + }else{ + print('Its A Not Palindrome'); + } + +} From 22ae678f8a1319ab51d065473be3c65070d99734 Mon Sep 17 00:00:00 2001 From: Satyam Shukla <61821492+satyamnoob@users.noreply.github.com> Date: Sat, 8 Oct 2022 11:08:41 +0530 Subject: [PATCH 2/2] Added tests --- strings/palindrome.dart | 97 +++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 14 deletions(-) diff --git a/strings/palindrome.dart b/strings/palindrome.dart index 837046f5..642ed81b 100644 --- a/strings/palindrome.dart +++ b/strings/palindrome.dart @@ -1,16 +1,85 @@ -void main(){ -print('Enter Words or number'); - // User enter a string or a number - String? original = 'hannah'; - - // then we will reverse the input - String? reverse = original.split('').reversed.join(''); - // then we will compare - if(original == reverse) - { - print('Its A Palindrome'); - }else{ - print('Its A Not Palindrome'); +import 'package:test/test.dart'; + +void main() { + palindrome(); +} + +void palindrome() { + print('Enter Words or number'); + // User enter a string or a number + String original = 'hannah'; + + // then we will reverse the input + String reverseUsingLoop = reverseStringUsingLoop(original); + String reverseUsingSplit = reverseStringUsingSplit(original); + String reverseUsingRunes = reverseStringUsingRunes(original); + String reverseUsingCodeUnits = reverseStringUsingCodeUnits(original); + + // then we will compare (Use any one of the four) + if (original == reverseUsingLoop) { + // print('Its A Palindrome'); + } else { + // print('Its A Not Palindrome'); + } + + if (original == reverseUsingSplit) { + // print('Its A Palindrome'); + } else { + // print('Its A Not Palindrome'); + } + + if (original == reverseUsingRunes) { + // print('Its A Palindrome'); + } else { + // print('Its A Not Palindrome'); } - + + if (original == reverseUsingCodeUnits) { + // print('Its A Palindrome'); + } else { + // print('Its A Not Palindrome'); + } + + // test + group('Palindrome Check', () { + test('Using loop to reverse a string.', () { + var string = 'hannah'; + expect(string, equals(reverseStringUsingLoop(string))); + }); + test('Using split to reverse a string.', () { + var string = 'hannah'; + expect(string, equals(reverseStringUsingSplit(string))); + }); + test('Using runes to reverse a string.', () { + var string = 'hannah'; + expect(string, equals(reverseStringUsingRunes(string))); + }); + test('Using code units to reverse a string.', () { + var string = 'hannah'; + expect(string, equals(reverseStringUsingCodeUnits(string))); + }); + }); +} + +String reverseStringUsingLoop(String input) { + String reversed = ""; + for (int index = input.length - 1; index >= 0; index--) { + reversed += input[index]; + } + return reversed; +} + +String reverseStringUsingSplit(String input) { + var chars = input.split(''); + return chars.reversed.join(); +} + +String reverseStringUsingRunes(String input) { + var chars = input.runes.toList(); + + return String.fromCharCodes(chars.reversed); +} + +String reverseStringUsingCodeUnits(String input) { + return String.fromCharCodes(input.codeUnits.reversed); }