Daily C Language Challenge No. 23: How Many Ways Can You Determine if a Number is a Palindrome?

๐Ÿ“Œ Problem Description

Please enter an integer x and determine whether it is a palindrome (i.e., it reads the same forwards and backwards).

Requirements๏ผš

  • Do not use string conversion

  • Handle negative numbers (e.g., -121 is not a palindrome)

  • Advanced: Avoid reversing the entire number to optimize time complexity

Example:

Input: 121 โ†’ Output: is a palindrome  Input: -121 โ†’ Output: is not a palindrome  Input: 12321 โ†’ Output: is a palindrome   

Difficulty:โญโญ

๐Ÿ’ก Basic Implementation (Reverse the entire number)

#include <stdio.h>#include <stdbool.h>
bool isPalindrome(int x) {    if (x < 0) return false; // Negative numbers return false directly    long reversed = 0;        // Prevent overflow during reversal    int original = x;
    while (x > 0) {        reversed = reversed * 10 + x % 10;        x /= 10;    }    return reversed == original;}
int main() {    int x;    printf("Input integer:");    scanf("%d", &x);    printf("%d %s palindrome", x, isPalindrome(x) ? "is" : "is not");    return 0;}

๐Ÿ”ฅ Advanced Optimization (Reverse half the number)

Avoid reversing the entire number to prevent overflow:

bool isPalindrome_opt(int x) {    if (x < 0 || (x % 10 == 0 && x != 0)) return false; // Handle negative numbers and cases ending with 0    int reversedHalf = 0;    // Stop the loop when the original number <= reversed half    while (x > reversedHalf) {        reversedHalf = reversedHalf * 10 + x % 10;        x /= 10;    }    // Even length: x == reversedHalf    // Odd length: x == reversedHalf / 10    return x == reversedHalf || x == reversedHalf / 10;}

Key Points๏ผš

  • Time Complexity O(logโ‚โ‚€n): Only reverse half the number

  • Space Complexity O(1): No extra memory required

๐Ÿ”ฅ Extended Features

1. Comparison of String Methods

#include <string.h>bool isPalindrome_str(int x) {    char str[20];    sprintf(str, "%d", x);    int len = strlen(str);    for (int i = 0; i < len/2; i++) {        if (str[i] != str[len-1-i]) return false;    }    return true;}

2. Handling Large Numbers (long long type)

bool isPalindrome_ll(long long x) {    if (x < 0) return false;    long long reversed = 0, original = x;    while (x > 0) {        reversed = reversed * 10 + x % 10;        x /= 10;    }    return reversed == original;}

๐Ÿค” Common Problem Analysis

Problem Error Cause Solution
Overflow not handled Reversal exceeds int range Use<span>long</span> type to store the reversed value
Misjudgment of numbers ending with 0 e.g., x=10 returns true Add<span>x%10 ==0 && x!=0</span> check
Incorrect judgment of odd-length numbers Middle digit not handled Allow<span>reversedHalf/10</span> when reversing half

๐Ÿš€ Upcoming Preview

No. 24: Finding Duplicate Numbers in an Array

๐ŸŽฏ Extended Challenge

Try to implement the following features (choose any one)๏ผš

  1. Recursive implementation of palindrome checking

  2. Count all palindromes within a range (e.g., 1~10000)

  3. Check palindrome strings (e.g., “abcba”)

๐Ÿ“ข Interaction TimeWhat pitfalls have you encountered when checking for palindromes? Feel free to leave a comment!

๐Ÿš€If you find this useful, feel free to share it with friends learningC language!

Author of this article:Vv Computer Graduate Examination World (focusing on computer graduate examination tutoring for 8 years)

Original Statement: Please contact for authorization to reprint, infringement will be pursued.

Leave a Comment