palindrome Algorithm

A palindrome algorithm is a computational technique or a set of rules and procedures used to determine if a given sequence of characters, typically a word, phrase, or number, reads the same forward and backward. Palindromes are prevalent in various applications, such as natural language processing, data analysis, cryptography, and coding theory. The algorithm essentially compares the first and last characters of the input sequence, then moves inward until the middle of the sequence is reached. If all corresponding pairs of characters match, the sequence is considered a palindrome. Several implementations of palindrome algorithms exist, including iterative, recursive, and stack-based approaches, each with their own advantages and caveats in terms of efficiency, readability, and memory usage. A common implementation of the palindrome algorithm is an iterative approach that uses two pointers, one starting at the beginning of the sequence and the other at the end. The algorithm compares the characters at the positions indicated by the pointers and increments or decrements the pointers accordingly. If the characters do not match, the sequence is not a palindrome, and the algorithm terminates. If the pointers meet at the middle of the sequence, then the sequence is a palindrome. This approach has a time complexity of O(n/2), where n is the length of the input sequence, and a space complexity of O(1) as it only requires constant additional memory for the pointers. Other implementations, such as recursive and stack-based algorithms, may have higher space complexity due to the overhead of function calls or data structures, but may offer more concise or elegant solutions to the palindrome problem.
#include <stdio.h>
int main()
{
    int n, reversedInteger = 0, remainder, originalInteger;

    printf("Enter an integer: ");
    scanf("%d", &n);

    originalInteger = n;

    // reversed integer is stored in variable 
    while( n!=0 )
    {
        remainder = n%10;
        reversedInteger = reversedInteger*10 + remainder;
        n /= 10;
    }

    // palindrome if orignalInteger and reversedInteger are equal
    if (originalInteger == reversedInteger)
        printf("%d is a palindrome.", originalInteger);
    else
        printf("%d is not a palindrome.", originalInteger);
    
    return 0;
}

LANGUAGE:

DARK MODE: