What is Palindrome in C Programming?

# What is Palindrome in C Programming?

Updated on Mar 8, 2024 16:15 IST

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:
• “level”
• “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:

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

## 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 <stdio.h> \n \n #include \n \n <string.h> \n \n \n int main() { \n char string[100]; \n int length, i, j, flag = 1; \n \n printf("Enter a string: "); \n scanf("%s", string); \n \n length = strlen(string); \n \n for (i = 0, j = length - 1; i < j; i++, j--) { \n if (string[i] != string[j]) { \n flag = 0; \n break; \n } \n } \n \n if (flag) { \n printf(""%s" is a palindrome.\n", string); \n } else { \n printf(""%s" is not a palindrome.\n", string); \n } \n \n return 0; \n } \n \n </string.h> \n \n </stdio.h>Copy code```

Output:

` `
`Enter a string: wow"wow" is a palindrome.Copy code`

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 <stdio.h> \n \n #include \n \n <string.h> \n \n \n int isPalindrome(char str[], int l, int h) { \n if (l == h) return 1; //base case \n if (str[l] != str[h]) return 0; //not a palindrome \n if (l < h + 1) return isPalindrome(str, l + 1, h - 1); //recursive call \n return 1; //is a palindrome \n } \n \n int main() { \n char string[100]; \n int length, flag; \n \n printf("Enter a string: "); \n scanf("%s", string); \n \n length = strlen(string); \n flag = isPalindrome(string, 0, length - 1); // check the string \n \n if (flag) { \n printf(""%s" is a palindrome.\n", string); \n } else { \n printf(""%s" is not a palindrome.\n", string); \n } \n return 0; \n } \n \n </string.h> \n \n </stdio.h>Copy code```

Output:

` `
`Enter a string: rotator"rotator" is a palindrome.Copy code`

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 <stdio.h> \n \n #include \n \n <string.h> \n \n \n int isPalindrome(char str[]) { \n int l = strlen(str); \n char *front = str, *end = str + l - 1; \n while (front < end) { \n if (*front != *end) return 0; \n front++; \n end--; \n } \n return 1; \n } \n \n int main() { \n char string[100]; \n int flag; \n \n printf("Enter a string: "); \n scanf("%s", string); \n \n flag = isPalindrome(string); // check the string \n \n if (flag) { \n printf(""%s" is a palindrome.\n", string); \n } else { \n printf(""%s" is not a palindrome.\n", string); \n } \n return 0; \n } \n \n </string.h> \n \n </stdio.h>Copy code```

Output:

` `
`Enter a string: madam"madam" is a palindrome.Copy code`

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 <stdio.h> \n \n #include \n \n <string.h> \n \n \n int isPalindrome(char str[]) { \n int l = strlen(str); \n char stack[l]; \n int top = -1; \n for (int i = 0; i < l; i++) { \n stack[++top] = str[i]; \n } \n for (int i = 0; i < l; i++) { \n if (str[i] != stack[top--]) return 0; \n } \n return 1; \n } \n \n int main() { \n char string[100]; \n int flag; \n \n printf("Enter a string: "); \n scanf("%s", string); \n \n flag = isPalindrome(string); // check the string \n \n if (flag) { \n printf(""%s" is a palindrome.\n", string); \n } else { \n printf(""%s" is not a palindrome.\n", string); \n } \n return 0; \n } \n \n </string.h> \n \n </stdio.h>Copy code```

Output:

` `
`Enter a string: mom"mom" is a palindrome.Copy code`

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 <stdio.h> \n \n int isPalindrome(int num) { \n int originalNum = num; \n int reverseNum = 0; \n while (num > 0) { \n reverseNum = reverseNum * 10 + num % 10; \n num /= 10; \n } \n return originalNum == reverseNum; \n } \n \n int main() { \n int num; \n printf("Enter a number: "); \n scanf("%d", &num); \n \n if (isPalindrome(num)) { \n printf("%d is a palindrome.\n", num); \n } else { \n printf("%d is not a palindrome.\n", num); \n } \n \n return 0; \n } \n \n </stdio.h>Copy code```

Output:

` `
`Enter a number: 12211221 is a palindrome.Copy code`

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 <stdio.h> \n \n \n int reverse(int num); \n int isPalindrome(int num); \n int main() \n { \n int num; \n printf("Enter a number: "); \n scanf("%d", &num); \n if(isPalindrome(num) == 1) \n { \n printf("%d is a palindrome.\n", num); \n } \n else \n { \n printf("%d is not a palindrome.\n", num); \n } \n return 0; \n } \n \n int isPalindrome(int num) \n { \n if(num == reverse(num)) \n { \n return 1; \n } \n return 0; \n } \n int reverse(int num) \n { \n int rem; \n static int sum=0; \n if(num!=0){ \n rem=num; \n sum=sum*10+rem; \n reverse(num/10); \n } \n else \n return sum; \n return sum; \n } \n \n </stdio.h>Copy code```

Output:

` `
`Enter a number: 7686776867 is a palindrome.Copy code`

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 <stdio.h> \n \n \n int isPalindrome(int num) { \n int digits[10], len = 0; \n while (num > 0) { \n digits[len++] = num % 10; \n num /= 10; \n } \n for (int i = 0; i < len / 2; i++) { \n if (digits[i] != digits[len - i - 1]) return 0; \n } \n return 1; \n } \n \n int main() { \n int num; \n printf("Enter a number: "); \n scanf("%d", &num); \n \n if (isPalindrome(num)) { \n printf("%d is a palindrome.\n", num); \n } else { \n printf("%d is not a palindrome.\n", num); \n } \n \n return 0; \n } \n \n </stdio.h>Copy code```

Output:

` `
`Enter a number: 4555445554 is a palindrome.Copy code`

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

Introduction to For Loop in C
A loop is one of the most fundamental logical structures in computer programming. Defining loops in code allows computers to repeat specific tasks. Defining the loop in a computer program...read more
Recursion in C++
Recursion in C++ is a method that calls itself repeatedly until a predetermined condition is satisfied. A recursive function has a base case and a recursive condition, and it repeatedly...read more
All About While Loop in C
Have you ever wondered how a while loop works in C programming? A control flow statement allows code to be executed repeatedly based on a given Boolean condition. The while...read more

### 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.