Palindrome in C: What Is Palindrome in C & How to Check It

Detecting a palindrome in a c string is a very common coding interview question that relies on understanding of the modulo arithmetic and two pointer string traversal technique. When defining what is palindrome in c, a palindrome in c is a number or string which can be read the same way forward and backward like 12321 or radar. When learning how to check palindrome in c for an integer, the idea is to extract the last digit of 'n' using n % 10 then add it to reversed accumulator using rev = rev * 10 + digit, then divide 'n' by 10 till it becomes zero and compare the two numbers ‘n' and ‘rev'.

### Palindrome in C: Integer Reversal & Two-Pointer String Check

Detecting a palindrome in a c string is a very common coding interview question that relies on understanding of the modulo arithmetic and two pointer string traversal technique. When defining what is palindrome in c, a palindrome in c is a number or string which can be read the same way forward and backward like 12321 or radar. When learning how to check palindrome in c for an integer, the idea is to extract the last digit of 'n' using n % 10 then add it to reversed accumulator using rev = rev * 10 + digit, then divide 'n' by 10 till it becomes zero and compare the two numbers ‘n' and ‘rev'.

Interactive Runnable C Implementation

#include <stdio.h>

int is_palindrome_int(int n) {
    if (n < 0) return 0;
    int original = n, rev = 0;
    while (n > 0) {
        rev = rev * 10 + (n % 10);
        n /= 10;
    }
    return original == rev;
}

int main(void) {
    int num = 12321;
    printf("%d is %s\n", num, is_palindrome_int(num) ? "a palindrome" : "not a palindrome");
    return 0;
}

Open example in compiler

Expected Output

12321 is a palindrome