palindrome program in c using strings code example

Example 1: palindrome program in c

#include 
int main() {
    int n, reversedN = 0, remainder, originalN;
    printf("Enter an integer: ");
    scanf("%d", &n);
    originalN = n;

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

    // palindrome if orignalN and reversedN are equal
    if (originalN == reversedN)
        printf("%d is a palindrome.", originalN);
    else
        printf("%d is not a palindrome.", originalN);

    return 0;
}

Example 2: Write a C program to check whether the string is a palindrome without using string functions.

#include #include  int main(){   char text[100];   int begin, middle, end, length = 0;    gets(text);    while ( text[length] != '\0' )      length++;    end = length - 1;   middle = length/2;    for( begin = 0 ; begin < middle ; begin++ )   {      if ( text[begin] != text[end] )      {         printf("Not a palindrome.\n");         break;      }      end--;   }   if( begin == middle )      printf("Palindrome.\n");    return 0;}

Tags:

Misc Example