What is Palindrome in C Programming?
A palindrome is a word, phrase, number, or sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example:
• “madam”
• “level”
• “radar”
• “civic”
• “refer”
• “rotator”
Palindrome words can also be phrases such as “A man, a plan, a canal: Panama”.
In this article, we will discuss how we check string and number palindromes in C programming. We will be covering the following sections today:
- What is Palindrome?
- Checking if a String is a Palindrome in C
- Checking if a Number is a Palindrome in C
So, without further ado, let’s get started!
What is Palindrome?
In programming, palindromes are used in a variety of ways. One common use is in algorithms for checking if a given word or phrase is a palindrome. This can be useful for various tasks such as spell-checking, natural language processing, and text analysis. Palindrome algorithms can also be used in data compression and encryption to detect and correct errors in transmitted data. Additionally, palindrome algorithms can be used in solving mathematical problems and puzzles, such as finding the largest palindrome in a given sequence of numbers.
Explore programming courses
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
Checking if a String is a Palindrome in C
Few methods you can use to check if the given string is a palindrome or not:
Let’s look at examples of each method below.
Using Loop
One way to check if a string is a palindrome is to compare the first and last characters of the string, then the second and second-to-last characters, and so on. If all the pairs of characters match, the string is a palindrome. This can be done using loops in C. Here’s an example of a C program that checks if a string is a palindrome:
#include
\n \n <stdio.h> \n \n \n \n #include \n \n \n \n <string.h> \n \n \n \n
\n \n int main() { \n \n char string[100]; \n \n int length, i, j, flag = 1; \n \n
\n \n printf("Enter a string: "); \n \n scanf("%s", string); \n \n
\n \n length = strlen(string); \n \n
\n \n for (i = 0, j = length - 1; i < j; i++, j--) { \n \n if (string[i] != string[j]) { \n \n flag = 0; \n \n break; \n \n } \n \n } \n \n
\n \n if (flag) { \n \n printf(""%s" is a palindrome.\n", string); \n \n } else { \n \n printf(""%s" is not a palindrome.\n", string); \n \n } \n \n
\n \n return 0; \n \n } \n \n \n \n </string.h> \n \n
\n \n </stdio.h>
Output:
Enter a string: wow"wow" is a palindrome.
In this program, we first declare a variable string to store the input string. The strlen() function from the string.h library is used to find the length of the string. We then use a for loop that compares the first and last characters of the string, then the second and second-to-last characters, and so on. If all the pairs of characters match, the variable flag is set to 1, otherwise, it is set to 0.
Finally, we check the value of flag to determine whether the string is a palindrome or not. If flag is 1, the string is a palindrome, otherwise, it is not.
Using Recursion
Another method to check if a string is a palindrome is by using recursion. The basic idea is to compare the first and last characters of the string, and then recursively check the substring formed by removing the first and last characters. If the substring is also a palindrome, then the original string is also a palindrome.
Here’s an example of a C program that checks if a string is a palindrome using recursion:
#include
\n \n <stdio.h> \n \n \n \n #include \n \n \n \n <string.h> \n \n \n \n
\n \n int isPalindrome(char str[], int l, int h) { \n \n if (l == h) return 1; //base case \n \n if (str[l] != str[h]) return 0; //not a palindrome \n \n if (l < h + 1) return isPalindrome(str, l + 1, h - 1); //recursive call \n \n return 1; //is a palindrome \n \n } \n \n
\n \n int main() { \n \n char string[100]; \n \n int length, flag; \n \n
\n \n printf("Enter a string: "); \n \n scanf("%s", string); \n \n
\n \n length = strlen(string); \n \n flag = isPalindrome(string, 0, length - 1); // check the string \n \n
\n \n if (flag) { \n \n printf(""%s" is a palindrome.\n", string); \n \n } else { \n \n printf(""%s" is not a palindrome.\n", string); \n \n } \n \n return 0; \n \n } \n \n \n \n </string.h> \n \n
\n \n </stdio.h>
Output:
Enter a string: rotator"rotator" is a palindrome.
In this program, we first declare a variable string to store the input string, and flag variable to store the result of the isPalindrome function. The strlen() function from the string.h library is used to find the length of the string.
Then we call the isPalindrome function with the input string and 0 as the first index and length-1 as the last index of the string. This function takes the input string and compares the first and last characters of the string, then the second and second-to-last characters, and so on recursively. If all the pairs of characters match, it returns 1, otherwise, it returns 0.
Finally, we check the value of flag to determine whether the string is a palindrome or not. If flag is 1, the string is a palindrome.
Using Pointers
Another method to check if a string is a palindrome is by using pointers. The basic idea is to compare the first and last characters of the string using two pointers, then move the pointers toward the centre of the string and repeat the comparison.
Here’s an example of a C program that checks if a string is a palindrome using pointers:
#include
\n \n <stdio.h> \n \n \n \n #include \n \n \n \n <string.h> \n \n \n \n
\n \n int isPalindrome(char str[]) { \n \n int l = strlen(str); \n \n char *front = str, *end = str + l - 1; \n \n while (front < end) { \n \n if (*front != *end) return 0; \n \n front++; \n \n end--; \n \n } \n \n return 1; \n \n } \n \n
\n \n int main() { \n \n char string[100]; \n \n int flag; \n \n
\n \n printf("Enter a string: "); \n \n scanf("%s", string); \n \n
\n \n flag = isPalindrome(string); // check the string \n \n
\n \n if (flag) { \n \n printf(""%s" is a palindrome.\n", string); \n \n } else { \n \n printf(""%s" is not a palindrome.\n", string); \n \n } \n \n return 0; \n \n } \n \n \n \n </string.h> \n \n
\n \n </stdio.h>
Output:
Enter a string: madam"madam" is a palindrome.
In this program, we first declare a variable string to store the input string, and flag variable to store the result of the isPalindrome function.
Then we call the isPalindrome function with the input string. This function takes the input string and uses two pointers, front and end to compare the first and last characters of the string, then the second and second-to-last characters, and so on. The pointer front is initialized to the first character of the string, and the pointer end is initialized to the last character of the string.
Then the function uses a while loop, moving front pointer to the next character and end pointer to the previous character in each iteration, comparing both pointers if they are equal or not. If all the characters match, it returns 1, otherwise, it returns 0.
Finally, we check the value of flag to determine whether the string is a palindrome or not. If flag is 1, the string is a palindrome.
Using Stack
One can check if a string is a palindrome by using a stack. The basic idea is to push the characters of the string one by one into a stack and then pop the characters one by one from the stack. If the original string and the popped string are the same, then the string is a palindrome.
Here’s an example of a C program that checks if a string is a palindrome using stack:
#include
\n \n <stdio.h> \n \n \n \n #include \n \n \n \n <string.h> \n \n \n \n
\n \n int isPalindrome(char str[]) { \n \n int l = strlen(str); \n \n char stack[l]; \n \n int top = -1; \n \n for (int i = 0; i < l; i++) { \n \n stack[++top] = str[i]; \n \n } \n \n for (int i = 0; i < l; i++) { \n \n if (str[i] != stack[top--]) return 0; \n \n } \n \n return 1; \n \n } \n \n
\n \n int main() { \n \n char string[100]; \n \n int flag; \n \n
\n \n printf("Enter a string: "); \n \n scanf("%s", string); \n \n
\n \n flag = isPalindrome(string); // check the string \n \n
\n \n if (flag) { \n \n printf(""%s" is a palindrome.\n", string); \n \n } else { \n \n printf(""%s" is not a palindrome.\n", string); \n \n } \n \n return 0; \n \n } \n \n \n \n </string.h> \n \n
\n \n </stdio.h>
Output:
Enter a string: mom"mom" is a palindrome.
In this program, we first declare a variable string to store the input string, and flag variable to store the result of the isPalindrome function. Then we call the isPalindrome function with the input string. This function takes the input string and uses a stack to store the characters of the string one by one. Then the function uses a for loop, popping the characters one by one from the stack and comparing them to the original string characters. If all the characters match, it returns 1, otherwise, it returns 0.
Finally, we check the value of flag to determine whether the string is a palindrome or not. If flag is 1, the string is a palindrome, otherwise, it is not.
Note that these approaches don’t take care of spaces, punctuation and capitalization. To make it case-insensitive and ignore spaces and punctuation, you can use tolower() and isalnum() functions from ctype.h library to convert all characters to lowercase and check if they are alphanumeric before passing the string to the function.
Checking if a Number is a Palindrome in C
Few methods you can use to check if a given number is a palindrome or not:
Let’s look at examples of each method below.
Using Modulus and Division
One can check if a number is a palindrome by using modulus and division. The basic idea is to get the last digit of the number by using the modulus operator, then remove the last digit by using the division operator, and repeat the process until all digits are compared. If all the digits match, the number is a palindrome.
Here’s an example of a C program that checks if a number is a palindrome:
#include
\n \n <stdio.h> \n \n \n \n int isPalindrome(int num) { \n \n int originalNum = num; \n \n int reverseNum = 0; \n \n while (num > 0) { \n \n reverseNum = reverseNum * 10 + num % 10; \n \n num /= 10; \n \n } \n \n return originalNum == reverseNum; \n \n } \n \n
\n \n int main() { \n \n int num; \n \n printf("Enter a number: "); \n \n scanf("%d", &num); \n \n
\n \n if (isPalindrome(num)) { \n \n printf("%d is a palindrome.\n", num); \n \n } else { \n \n printf("%d is not a palindrome.\n", num); \n \n } \n \n
\n \n return 0; \n \n } \n \n
\n \n </stdio.h>
Output:
Enter a number: 12211221 is a palindrome.
In this program, we first declare a variable num to store the input number, and originalNum variable to store the original value of the input number. Then we call the isPalindrome function with the input number, this function takes the input number and uses a while loop to reverse the number and store it in reverseNum variable, and then compares the originalNum with the reverseNum if they are equal or not. If they are equal then the number is a palindrome; otherwise not.
Finally, we check the value of the function return value to determine whether the number is a palindrome or not. And print the result.
Using Recursion
Another method to check if a number is a palindrome is by using recursion. The basic idea is to get the last digit of the number by using the modulus operator, then remove the last digit by using the division operator and recursively check the remaining digits. If all the digits match, the number is a palindrome.
Here’s an example of a C program that checks if a number is a palindrome using recursion:
#include
\n \n <stdio.h> \n \n \n \n
\n \n int reverse(int num); \n \n int isPalindrome(int num); \n \n int main() \n \n { \n \n int num; \n \n printf("Enter a number: "); \n \n scanf("%d", &num); \n \n if(isPalindrome(num) == 1) \n \n { \n \n printf("%d is a palindrome.\n", num); \n \n } \n \n else \n \n { \n \n printf("%d is not a palindrome.\n", num); \n \n } \n \n return 0; \n \n } \n \n
\n \n int isPalindrome(int num) \n \n { \n \n if(num == reverse(num)) \n \n { \n \n return 1; \n \n } \n \n return 0; \n \n } \n \n int reverse(int num) \n \n { \n \n int rem; \n \n static int sum=0; \n \n if(num!=0){ \n \n rem=num; \n \n sum=sum*10+rem; \n \n reverse(num/10); \n \n } \n \n else \n \n return sum; \n \n return sum; \n \n } \n \n
\n \n </stdio.h>
Output:
Enter a number: 7686776867 is a palindrome.
The above program contains two functions, reverse(int num) and isPalindrome(int num). The reverse function takes an input of a number and returns its reverse. The isPalindrome function takes a number and passes it to the reverse function. It then compares the original number to the value returned by the reverse function. If they are equal, the isPalindrome function returns 1, otherwise it returns 0. The main function captures this return value and uses it to print the appropriate output.
Using Array
Yet another method to check if a number is a palindrome is by using an array in C programming is to convert the number into an array of digits, then compare the first and last elements, the second and second-to-last elements, and so on. Here’s an example of a C program that checks if a number is a palindrome using C arrays:
#include
\n \n <stdio.h> \n \n \n \n
\n \n int isPalindrome(int num) { \n \n int digits[10], len = 0; \n \n while (num > 0) { \n \n digits[len++] = num % 10; \n \n num /= 10; \n \n } \n \n for (int i = 0; i < len / 2; i++) { \n \n if (digits[i] != digits[len - i - 1]) return 0; \n \n } \n \n return 1; \n \n } \n \n
\n \n int main() { \n \n int num; \n \n printf("Enter a number: "); \n \n scanf("%d", &num); \n \n
\n \n if (isPalindrome(num)) { \n \n printf("%d is a palindrome.\n", num); \n \n } else { \n \n printf("%d is not a palindrome.\n", num); \n \n } \n \n
\n \n return 0; \n \n } \n \n
\n \n </stdio.h>
Output:
Enter a number: 4555445554 is a palindrome.
In this program, we first define a function isPalindrome(int num) that takes an integer as an input and returns an integer, this function is used to check if the input number is a palindrome or not.
The function converts the input number into an array of digits, then compares the first and last elements, the second and second-to-last elements, and so on. If all the digits match, the function returns 1, otherwise it returns 0. The main function captures this return value and uses it to print the appropriate output.
Explore free C programming courses
Endnotes
I hope that this article was useful in helping you learn how to check for number and string palindromes in string programming using various methods. Explore our C articles to find out more about the language and consolidate your knowledge of the fundamentals.
_____________________________________________
Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.
Click here to submit its review.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio