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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if any anagram of a string is palindrome or not
- Check given string is oddly palindrome or not
- Check if a given string is a rotation of a palindrome
- C Program to Check if a Given String is Palindrome
- Check whether the given string is Palindrome using Stack
- Check if it is possible to create a palindrome string from given N
- Check given string is oddly palindrome or not | Set 2
- Given two strings check which string makes a palindrome first
- Check if there exists any sub-sequence in a string which is not palindrome
- Java program to check whether a string is a Palindrome
- Python program to check if a string is palindrome or not
- Recursive function to check if a string is palindrome
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Python program to check if given string is vowel Palindrome
- Check if a string can be rearranged to form special palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- TCP Client-Server Program to Check if a Given String is Palindrome
- Check if string is palindrome after removing all consecutive duplicates
- Check if string remains palindrome after removing given number of characters
- Check if the characters in a string form a Palindrome in O(1) extra space
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.