You are here

Palindrome in C

C program to check if a string or a number is palindrome or not. What is a palindrome string? It is a string that reads the same backward as well as forward and can be of odd or even length. A palindrome number is a number which is equal to its reverse. Our program works as follows: at first, we copy the entered string into a new string, and then we reverse it and compare it with original string. If both of them have the same sequence of characters, i.e., they are identical then the string is a palindrome otherwise not. To perform copy, reverse and compare operations we use strcpy, strrev and strcmp functions of "string.h" respectively. C program to check palindrome without using string functions. Some palindrome strings are: "C", "CC", "madam", "ab121ba", "C++&&++C".

Palindrome program in C language

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main()
  5. {
  6.   char a[100], b[100];
  7.  
  8.   printf("Enter a string to check if it is a palindrome\n");
  9.   gets(a);
  10.  
  11.   strcpy(b, a);  // Copying input string
  12.   strrev(b);  // Reversing the string
  13.  
  14.   if (strcmp(a, b) == 0)  // Comparing input string with the reverse string
  15.     printf("The string is a palindrome.\n");
  16.   else
  17.     printf("The string isn't a palindrome.\n");
  18.  
  19.   return 0;
  20. }

Output of C palindrome program:
Palindrome program in C output

Download palindrome program.

C program for palindrome without using string functions

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.   char text[100];
  6.   int begin, middle, end, length = 0;
  7.  
  8.   gets(text);
  9.  
  10.   while (text[length] != '\0')
  11.     length++;
  12.  
  13.   end = length - 1;
  14.   middle = length/2;
  15.  
  16.   for (begin = 0; begin < middle; begin++)
  17.   {
  18.     if (text[begin] != text[end])
  19.     {
  20.       printf("Not a palindrome.\n");
  21.       break;
  22.     }
  23.     end--;
  24.   }
  25.   if (begin == middle)
  26.     printf("Palindrome.\n");
  27.  
  28.   return 0;
  29. }

Palindrome number in C

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int n, r = 0, t;
  6.  
  7.    printf("Enter an integer to check if it is palindrome or not\n");
  8.    scanf("%d", &n);
  9.  
  10.    t = n;
  11.  
  12.    while (t != 0)
  13.    {
  14.       r = r * 10;
  15.       r = r + t%10;
  16.       t = t/10;
  17.    }
  18.  
  19.    if (n == r)
  20.       printf("%d is a palindrome number.\n", n);
  21.    else
  22.       printf("%d isn't a palindrome number.\n", n);
  23.  
  24.    return 0;
  25. }

C palindrome program using functions and pointers

  1. #include <stdio.h>
  2.  
  3. int is_palindrome(char*);
  4. void copy_string(char*, char*);
  5. void reverse_string(char*);
  6. int string_length(char*);
  7. int compare_string(char*, char*);
  8.  
  9. int main()
  10. {
  11.    char string[100];
  12.    int result;
  13.  
  14.    printf("Input a string\n");
  15.    gets(string);
  16.  
  17.    result = is_palindrome(string);
  18.  
  19.    if (result == 1)
  20.       printf("\"%s\" is a palindrome string.\n", string);
  21.    else
  22.       printf("\"%s\" isn't a palindrome string.\n", string);
  23.  
  24.    return 0;
  25. }
  26.  
  27. int is_palindrome(char *string)
  28. {
  29.    int check, length;
  30.    char *reverse;
  31.  
  32.    length = string_length(string);    
  33.    reverse = (char*)malloc(length+1);    
  34.  
  35.    copy_string(reverse, string);
  36.    reverse_string(reverse);
  37.  
  38.    check = compare_string(string, reverse);
  39.  
  40.    free(reverse);
  41.  
  42.    if (check == 0)
  43.       return 1;
  44.    else
  45.       return 0;
  46. }
  47.  
  48. int string_length(char *string)
  49. {
  50.    int length = 0;  
  51.  
  52.    while(*string)
  53.    {
  54.       length++;
  55.       string++;
  56.    }
  57.  
  58.    return length;
  59. }
  60.  
  61. void copy_string(char *target, char *source)
  62. {
  63.    while(*source)
  64.    {
  65.       *target = *source;
  66.       source++;
  67.       target++;
  68.    }
  69.    *target = '\0';
  70. }
  71.  
  72. void reverse_string(char *string)
  73. {
  74.    int length, c;
  75.    char *begin, *end, temp;
  76.  
  77.    length = string_length(string);
  78.  
  79.    begin = string;
  80.    end = string;
  81.  
  82.    for (c = 0; c < (length - 1); c++)
  83.        end++;
  84.  
  85.    for (c = 0; c < length/2; c++)
  86.    {        
  87.       temp = *end;
  88.       *end = *begin;
  89.       *begin = temp;
  90.  
  91.       begin++;
  92.       end--;
  93.    }
  94. }
  95.  
  96. int compare_string(char *first, char *second)
  97. {
  98.    while(*first==*second)
  99.    {
  100.       if (*first == '\0' || *second == '\0')
  101.          break;
  102.  
  103.       first++;
  104.       second++;
  105.    }
  106.    if (*first == '\0' && *second == '\0')
  107.       return 0;
  108.    else
  109.       return -1;
  110. }

A palindrome string can be a single word or a phrase or a sentence. Pointers are used in functions; you can write the program without using them.