ππIntermediate
Check Palindrome Using Pointers
Palindrome with pointers
A palindrome reads the same forwards and backwards. Using two pointers (start and end), we can efficiently check if a string is a palindrome.
π Examples
β "radar"
Is palindrome
β "hello"
Not palindrome
C Program to Check Palindrome Using Pointers
palindrome_pointers.c
C
1#include <stdio.h>2#include <string.h>3#include <stdbool.h>45bool isPalindrome(char *str) {6 char *start = str;7 char *end = str + strlen(str) - 1;8 9 while (start < end) {10 if (*start != *end) {11 return false;12 }13 start++; // Move forward14 end--; // Move backward15 }16 return true;17}1819int main() {20 char str1[] = "radar";21 char str2[] = "hello";22 23 printf("\"%s\" is %s\n", str1, 24 isPalindrome(str1) ? "a palindrome" : "NOT a palindrome");25 printf("\"%s\" is %s\n", str2, 26 isPalindrome(str2) ? "a palindrome" : "NOT a palindrome");27 28 return 0;29}Output
"radar" is a palindrome
"hello" is NOT a palindrome
π Two-Pointer Technique
Checking "radar":
startβr a d a rβend β r == r
startβa d aβend β a == a
startβdβend β middle reached
Result: Palindrome!
π― Key Takeaways
βTwo pointers: start and end
βCompare *start with *end
βMove towards center
βStop when start >= end
Related Examples
Want to Learn More?
Explore our comprehensive tutorials for in-depth explanations of C programming concepts.
Browse Tutorials