Palindrome program in C
Written by
Palindrome in C Program
What is a palindrome?
- A palindrome is nothing but a text or string which when read forward or backward is read the same. It can contain any type of characters.
- There are even palindrome numbers which are numbers which are the same when read forward and backward.
- For e.g: MADAM, NAN, 12321, ANNA etc.
- Note that, the palindrome can have even or odd number of characters.
Approach 1: Using the inbuilt C functions from string.h:
- We first scan the string.
- We then copy it to another string as well.
- We reverse the copied string here it is string 2 using strrev
- We compare the characters of both strings using strcmp; to check if the strung read forward and backward reads same or not.
- This is not recommended approach as strrev is from Microsoft C library and does not work in standard C library. If you are using a Turbo C++ IDE, only then will the below approach work. The GNU library has no such function defined and will throw you an error.
- Here to scan the string we are using fgets method in order to understand how it is used, although it is not recommended.
Code:
#include<conio.h>
#include <string.h> //include the header file which contains definition of string functions
int main()
{
clrscr();
char string1[50], string2[50] ;
printf("Enter a string : \n");
fgets(string1, 100, stdin); //read string input from standard input screen.
strcpy(string2,string1); //copy string 1 into string 2
strrev(string2); //reverse string 2 in order to compare both string in next step
if( strcmp(string1, string2) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
getch();
return 0;
}
Output:
Case 1: String is a palindrome – odd numbered.
Enter a string: MADAM
Entered string is a palindrome.
Case 2: String is a palindrome – even numbered.
Enter a string: 12ANNA21
Entered string is a palindrome.
Case 3: String is not a palindrome.
Enter a string: STUDYMITE
Entered string is not a palindrome.
Approach 2: Without using pre-defined functions of C:
- In this approach we will first accept the input string.
- Next, we calculate the length of the string.
- We know that palindrome should be read same forward and backward.
- Hence, we divide the array into two and traverse the elements while always comparing the elements of the first section and the second section to match with each other proceeding sequential.
- It shall work for both odd and even palindromes; as for even palindromes the string will be equally divided and each element compared; whereas for odd palindromes we will compare all elements except the middle element.
Code:
#include <stdio.h>
int main()
{
char string[100];
int start, mid, end, size = 100, bytes_read, length, i, j;
printf("Enter a string: ");
string = (char *) malloc (size);
bytes_read = getline (&string, &size, stdin);
length = bytes_read - 1; //since getline() stores '\n' at last block. hence bytesread - 1 = length of string.
end = length - 1;
mid = length/2;
for (start = 0; start < mid; start++, end--)
{
if (string[start] != string[end])
{
printf("Entered string is not a Palindrome.\n");
break;
}
}
if (start == mid)
printf("Entered string is a Palindrome.\n");
return 0;
}
Output:
Case 1: String is a palindrome – odd numbered.
Enter a string: POP
Entered string is a Palindrome.
Case 2: String is a palindrome – even numbered.
Enter a string: MADAM
Entered string is a Palindrome.
Case 3: String is not a palindrome.
Enter a string: STUDYMITE
Entered string is a Palindrome.
Approach 3: Program for number palindrome:
- Here we scan an integer as input.
- We then manually calculate the reverse of the number.
- Next, both the original numbers and its reverse are compared; on returning true it means it is a palindrome ; if false then it is not.
Code:
#include <stdio.h>
int main()
{
int num, rem = 0, rev, n ;
printf("Enter an integer to check if it is palindrome or not\n");
scanf("%d", &num);
n = num;
while (n != 0) //reversing the number
{
rem = n % 10;
rev = rem + ( rev* 10);
n = n/10;
}
if (num == rev) //comparing to check palindrome or not
printf("%d number is a palindrome.\n", num);
else
printf("%d number isn't a palindrome.\n", num);
return 0;
}
Output:
Enter an integer to check if it is palindrome or not
12321
12321 number is a palindrome.
Enter an integer to check if it is palindrome or not
12312
12312 number isn't a palindrome.
Approach 4: Program using pointers for palindrome:
- We have used a combination of functions and pointers to address the concern.
- We initially call a function check_palindrome; whose function is to return a flag to main() based on the comparison output of the string and its reverse.
- We have created separate functions for copying the original string, then reversing it and then comparing them.
Code:
#include <stdio.h>
int check_palindrome(char*, int);
void copy_string(char*, char*);
void reverse_string(char*, int);
int compare_string(char*, char*);
int main()
{
char *string;
int result, size = 100, bytes_read, l;
printf("Enter a string: ");
string = (char *) malloc (size);
bytes_read = getline (&string, &size, stdin);
l = bytes_read - 1;
result = check_palindrome(string, l);
if (result == 1)
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");
return 0;
}
int check_palindrome(char *string, int length)
{
int check;
char *reverse;
reverse = (char*)malloc(length+1);
copy_string(reverse, string);
reverse_string(reverse, length);
check = compare_string(string, reverse);
free(reverse);
if (check == 0)
return 1;
else
return 0;
}
void copy_string(char *dest, char *source)
{
while(*source)
{
*dest = *source;
source++;
dest++;
}
*dest = '\0';
}
void reverse_string(char *string, int length)
{
int c;
char *start, *end, temp;
start = string;
end = string;
for (c = 0; c < (length - 1); c++)
end++;
for (c = 0; c < length/2; c++)
{
temp = *end;
*end = *start;
*start = temp;
start++;
end--;
}
}
int compare_string(char *string, char *reverse)
{
while(*string==*reverse)
{
if (*string == '\0' || *reverse == '\0')
break;
string++;
reverse++;
}
if (*string == '\0' && *reverse == '\0')
return 0;
else
return -1;
}
Output:
Enter a string: madam
Entered string is a palindrome.
Enter a string: Neon
Entered string is not a palindrome.
Thus, we have seen various methods to perform string reverse operation in C.