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.
Algorithm:
Below is the implementation of the above approach:
// C program to check if a string is palindrome // using pointers #include // Function to check if the string is palindrome // using pointers void isPalindrome(char* string) { char *ptr, *rev; ptr = string; while (*ptr != '\0') { ++ptr; } --ptr; for (rev = string; ptr >= rev;) { if (*ptr == *rev) { --ptr; rev++; } else break; } if (rev > ptr) printf("String is Palindrome"); else printf("String is not a Palindrome"); } // Driver code int main() { char str[1000] = "madam"; isPalindrome(str); return 0; }
Output:
String is Palindrome