Check Palindrome in C++

Check Palindrome in C++

2 mins read259 Views Comment
Updated on Feb 16, 2023 18:31 IST

Learn numerous techniques for determining whether a string is a palindrome. Bypassing spaces, punctuation, and capitalization, you may quickly determine whether a string is a palindrome by using recursion and string manipulation.

2023_02_MicrosoftTeams-image-296-2.jpg

We’ll create an application to check palindrome in C++ in this blog post. A word, phrase, number, or other string of letters that reads the same both forward and backward is known as a palindrome (ignoring spaces, punctuation, and capitalization).

For instance, the words below are palindromes: 

  • “racecar” 
  • “level” 
  • “civic 
  • “noon” 

Problem: Create a method that accepts a parameterized string and returns whether the given string is a palindrome or not a palindrome. If the string is a palindrome, the technique should disregard capitalization, punctuation, and spaces. Any length of the string should be manageable by the procedure. 

You can also explore: New and delete operators in C++

Examples: 

  • Input: “civic” 
  • Output: true 
  • Input: “noon” 
  • Output: true 
  • Input: “Hello” 
  • Output: false 

Method1: C++ program to check palindrome with a loop 

The simplest method is to use the STL’s built-in reverse function. To solve the issue, adhere to the instructions listed below: 

  • Reverse the string Str after copying it to another string, let’s say new_str. 
  • Print “Yes” after verifying that the strings Str and new_str are equal. Print “No” if not. 

The application of the aforementioned strategy is seen below: 

2023_02_image-65.jpg

Complexity: 

  • Time Complexity: O(N) 
  • Auxiliary Space: O(N) 

You can also explore: ROUND() Function in C++

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

– / –
4 months
4.24 K
6 weeks
– / –
15 days
– / –
– / –
7.14 K
5 weeks
name
SoloLearnCertificateStar Icon4.5
Free
– / –
– / –
4 months
– / –
3 months

Method 2: Efficient Approach to check a palindrome 

By traversing the string and determining whether the character at index it is equal to the character at index (N-i-1) for each index in the range [0, N/2], the aforementioned method can be made less space-intensive. 

To solve the issue, adhere to the instructions listed below: 

  • Utilizing the variable i iterate across the range [0, N/2], checking each time if the character at index I and N-i-1 are not equivalent before printing “No” and breaking. 
  • Print “Yes” if none of the aforementioned situations apply. 

You can also explore: Understanding C++ Namespaces and its Methods

The application of the aforementioned strategy is seen below: 

2023_02_image-61.jpg

Complexity: 

  • Time Complexity: O(N) 
  • Auxiliary Space: O(1) 

Method 3: Check if the given number is palindrome or not recursively. 

The method for constructing the function is to call it again until the number has been traversed entirely from the rear. Use a temporary variable to save the number’s reverse using the formula we found in this post. Once num==0 is attained in the base case, the temp variable, which records the inverse of a number, is returned. 

You can also explore: What are Getline Function in C++?

The application of the aforementioned strategy is seen below: 

2023_02_image-64.jpg

Complexity: 

  • Time complexity: O(log10N). 
  • Auxiliary space: O(log10N). 
About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio