C Program to check if a number/string is palindrome or not

Kanchana Kariyawasam
3 min readOct 12, 2020

Hello everyone. This is my second article about the C language. Today I would like to present, how to code, to find whether is it Palindrome or not. In my code, I have included finding the palindrome of a Number and a String.

First of all, Let’s see what is a Palindrome. A palindrome is a word, number, phrase, or another sequence of characters that reads the same backward as forward, such as abba, madam. There are also numeric palindromes, like 12321, 5665. Sentence-length palindromes ignore capitalization, punctuation, and word boundaries.

To find a Palindrome of a string in my way, we want strlen() function. Because of that, we want string.h header file. I have coded this by using two separate functions. If you want, you can write it in the main function. But the best way is, writing separate functions. Because of that, you can find potential errors easily.

As you know, there are pre-processor directives and function prototypes in the above code. After that, there are the main function and other functions. In the main function, I have asked what do you want to search. Is it a Palindrome of a number or a String? I have used a character type variable for that. After your input, the compiler checks and go to the function that you are calling. If you input ’N’, then the compiler will call the pal_number() function. I have included a ternary operator to check it without using an if-else condition. In my opinion, when you are coding, your code should be short & sweet. Not only that but also a reader can be read your code without any difficulties. In the below, you can see my code. I think it will be very readable to you.

#include<stdio.h>
#include<string.h>
//function prototypes
void pal_number();
void pal_string();
void main()
{
char is_num; // char type variable to store a character that you input
printf(“What do you want to check ? If a Number press ’N’ & If a String press ‘S’ : “);
scanf(“%c”,&is_num);
is_num == ’N’ ? pal_number() : pal_string(); // Selection of “what do you want to search”
}
void pal_number()
{
int num;
printf(“Enter a number : “);
scanf(“%d”,&num);
int revNo = 0,temp = num;
while(num!=0)
{
revNo = revNo*10 + num%10;
num = num / 10;
}
revNo == temp ? printf(“Palindrome”) : printf(“Not a Palindrome”);
}
void pal_string()
{
char c[200];
printf(“Enter a sentence : “);
getchar();
gets(c);
int x = strlen(c)/2,y = strlen(c)-1,count = 0;
for(int i=0,j=y;i<x;i++,j--)
{
if(c[i]==c[j])
{
count++;
}
}
count == x ? printf(“Palindrome”) : printf(“Not a Palindrome”);
}

Now let’s discuss the logic of two functions (pal_string and pal_number). In pal_number, firstly I have included to input a number. Then I have declared and initialized variables. “temp” is a variable that contains a value of a number. Since the value of “num” changes as you go down, I have taken a temp variable to store it. “revNo” is a variable that is used to check equality of it and “temp” variable. If they are equal, the number that you have inputted is a Palindrome.

In pal_string, firstly I have included to input a string by using the gets() function. Previous, there is a getchar() function. In the main function, you should input a character (’N’ or ‘S’). If you have entered ‘S’, then the main function call pal_string() function. If there is not a getchar() function, then the compiler automatically assigns a value to it(char type variable-c) after calling that function. So, there is a problem. Because of that, I have included getchar(). By using a strlen() function, you can find a length of a string. In the last, there is a ternary operator, to print “Palindrome” or “Not Palindrome”.

So, I think you can get an idea about my code and my logic. If you think this is important, please give me a clap & share it with another person.

--

--

Kanchana Kariyawasam

Former Software Engineer Intern at Geveo-Australasia || Undergraduate of Faculty of Information Technology, University of Moratuwa.