Given a string. The task is to check if the string is a palindrome or not using pointers. You are not allowed to use any built-in string functions.
A string is said to be a palindrome if the reverse of the string is same as the original string. For example, “madam” is palindrome because when the string is reversed same string is achieved, but “Madam” is not a palindrome.
Input: str = "Madam" Output: String is not a Palindrome. Input: str = "madam" Output: String is Palindrome. Input: str = "radar" Output: String is Palindrome.
- Take two pointers say, ptr and rev.
- Initialize ptr to the base address of the string and move it forward to point to the last character of the string.
- Now, initialize rev to the base address of the string and start moving rev in forward direction and ptr in backward direction simultaneously until middle of the string is reached.
- If at any point the character pointed by ptr and rev does not match, then break from the loop.
- Check if ptr and rev crossed each other, i.e. rev > ptr. If so, then the string is palindrome otherwise not.
Below is the implementation of the above approach:
String is Palindrome
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- The length of a string using pointers
- C++ Program to compare two string using pointers
- Sentence Palindrome (Palindrome after removing spaces, dots, .. etc)
- Count all palindrome which is square of a palindrome
- Program to reverse an array using pointers
- C program to sort an array using pointers
- Check whether the given string is Palindrome using Stack
- Why C treats array parameters as pointers?
- Dangling, Void , Null and Wild Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Pointers vs References in C++
- What are near, far and huge pointers?
- Output of C programs | Set 64 (Pointers)
- Applications of Pointers in C/C++
- Features and Use of Pointers in C/C++
- Difference between Iterators and Pointers in C/C++ with Examples
- Chain of Pointers in C with Examples
- How many levels of pointers can we have in C/C++
- Why do we need reference variables if we have pointers
- Unusual behaviour with character pointers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.