Checking for Palindromes in Dart Programming

In software development, checking if a string or a number is a palindrome is a common task that helps developers practice working with strings and logic. A palindrome is a word, phrase, or number that reads the same backward as forward, such as "radar", "level", or "121".

In this post, we’ll explore how to write a simple Dart program to check if a given string is a palindrome.

Checking for Palindromes in Dart Programming

Getting Started

A palindrome is a word, phrase, number, or sequence that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.

It ignores punctuation, case, and spacing (depending on the context). In this basic example, we will consider exact character matches, including case and spacing. We’ll later show how to ignore case and non-alphabetic characters for a more flexible version.

Examples Palindromes

  • Word: madam, racecar, level, etc.
  • Number: 121,131,1331 etc.
  • Phrase: Do geese see God?, Madam, in Eden, I’m Adam, A man, a plan, a canal, Panama!

Check for Polindrome String

There are various ways to check whether a string is a palindrome. For example, you can use a built-in reverse function or implement a loop to reverse the string manually.

Useing Reverse Function

void CheckForPalindrome(String input) {
  // Remove spaces and convert to lowercase
  String cleaned = input.replaceAll(RegExp(r'\s+'), '').toLowerCase();

  // Compare the string with its reverse
  String reversed = cleaned.split('').reversed.join('');
  if(cleaned == reversed){
    print("\'$input\' is a Palindrome");
  }
  else
     print("\'$input\' is not a Palindrome");
}

void main() {
  CheckForPalindrome("Racecar");
  CheckForPalindrome("hello");
  CheckForPalindrome("A man a plan a canal Panama");
}

Output

'Racecar' is a Palindrome
'hello' is not a Palindrome
'A man a plan a canal Panama' is a Palindrome

Using Loop

void CheckForPalindrome(String input) {  
  // Remove spaces and convert to lowercase  
  String cleaned = input.replaceAll(RegExp(r'\s+'), '').toLowerCase();  
  // Compare the string with its reverse  
  int start = 0;  
  int end = cleaned.length - 1;  
  bool ispolindrome=true;  
   while (start < end) {  
   if (cleaned[start] != cleaned[end]) {  
    ispolindrome= false;  
    break;  
   }  
   start++;  
   end--;  
  }  
  if(ispolindrome == true){  
   print("\'$input\' is a Palindrome");  
  }  
  else  
    print("\'$input\' is not a Palindrome");  
 }  
 void main() {  
  CheckForPalindrome("Racecar"); 
  CheckForPalindrome("hello"); 
  CheckForPalindrome("A man a plan a canal Panama"); 
 }  

Output

'Racecar' is a Palindrome
'hello' is not a Palindrome
'A man a plan a canal Panama' is a Palindrome

Check for Polindrome Number

To check if a number is a palindrome in Dart, you can convert it to a string and compare it with its reverse or use arithmetic for a purely numeric solution. If you choose to convert the number to a string, you can then check whether it is a palindrome using this above givenmethod.

The example below demonstrates how to check whether a number is a palindrome using an arithmetic method.


void isNumberPalindrome(int number) {  
  // Negative numbers aren't palindromes  
  if (number < 0)   
  {  
   print("Please provide a positive number");  
   return;  
  }  
  int original = number;  
  int reversed = 0;  
  while (number > 0) {  
   int digit = number % 10;  
   reversed = reversed * 10 + digit;  
   number ~/= 10; // Integer division  
  }  
  if(original == reversed){  
   print("\'$original\' is a Palindrome");  
  }  
  else  
    print("\'$original\' is not a Palindrome");  
 }  
 void main() {  
  isNumberPalindrome(121);   
  isNumberPalindrome(123);   
  isNumberPalindrome(12321);  
  isNumberPalindrome(-121);   
 } 

OutPut

'121' is a Palindrome
'123' is not a Palindrome
'12321' is a Palindrome
Please provide a positive number

Summary

Checking for palindromes in Dart is a great way to get familiar with string manipulation and regular expressions. Whether you're working on a coding challenge, a quiz app, or natural language processing, this concept often comes in handy.

Dart makes it easy to implement both simple and advanced versions of a palindrome checker with a few lines of code,I hope it was helpful.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال