Palindrome Examples
In this article, we will explain how to do a C program of Palindrome to handle both Text & numbers.
What is a Palindrome? It is a word, sentence or a number, which looks same even when reversed. If you read it from backwards, then it will be same as read from first. For example: 12321 or Civic, Radar, rotator etc., This article has:
- C program to verify Palindrome
- VBA program for Palindrome
- Palindrome program in PHP (yet to publish)
Even we have few Palindrome examples in long sentences. But these are not yet handled in the Palindrome C program mentioned in this article, but the VBA & PHP does this effectively.
Palindrome Words
- Cigar? Toss it in a can. It is so tragic.
- Dammit, I’m mad.
- Madam, I’m Adam.
- Rise to vote, sir.
- Step on no pets.
- Was it a car or a cat I saw?
For more list of Palindrome words refer this link Rinkworks. It has a huge list of words and sentences. Now, lets see how to check whether a input number or a text is a palindrome or now by using a C program.
C program of Palindrome
It is been a tough day for me finalizing this code. I was struck in passing parameters to this sub routines mentioned in this C program for Palindrome. It took almost 3 hours to resolve the issues.
So, this code here has 2 separate function. One function to handle numeric palindrome & other for string Palindrome. Of course a string can handle numbers too, but still I wanted to make a separate function for it.
To execute this code, this is the syntax: Palindro <String/Number> <s/n> {Where ‘s’ – string, ‘n’-numer}.
Numeric & String Palindrome Program in C
//Palindro.c //Palindrome program in c from officetricks.com #include <stdio.h> #include <stdlib.h> //Palindrome Word prorgam in c void stringpalindrome(char argstr[]) { int strl,i; printf("\nInput Data Type is non-numberic"); strl = strlen(argstr); i=0; while (i<strl) { if(argstr[i] != argstr[strl-i-1]) { printf("\n%s is not a Palindrome",argstr); return; } i = i + 1; } printf("\n%s is a Palindrome",argstr); return; } //Palindrome number prorgam in c void numberpalindrome(unsigned long inputnum) { unsigned long tempnum,modnum,numreverse; printf("\nInput Data Type is numberic"); tempnum = inputnum; numreverse = 0; printf("\nChecking For Palindrome:%lu\n",inputnum); //Loop to Reverse the Number while (tempnum > 0 ) { modnum = tempnum % 10; numreverse = (numreverse * 10) + modnum; tempnum = (tempnum - modnum) / 10; } //If reversed numer is same, then it is confirmed that is Palindrome if(inputnum == numreverse) printf("\nInput Number is a Palindrome"); else printf("\nInput Number is not Palindrome"); return; } int main (int argc,char *argv[]) { //Print arguments passed to main program int i; printf("Officetricks.com - Parameters passed:"); for (i=1;i<argc;i++) printf("%s ;",argv[i]); //Check for error in Arguments if(argc != 3) { printf("\nSyntax Error. Proper Syntax: palindro <number/text> <s/n> {where 's'-string, 'n'-number}\n"); return(1); } //Call string or numeric Palindrome checking functions if (argv[2][0] == 'n') numberpalindrome(atol(argv[1])); if (argv[2][0] == 's') stringpalindrome(argv[1]); return(0); }
Compile this palindrome program in C and try it with different examples as per the syntax given. Please note that for numbers, the data types are defined in unsigned long to handle large numbers.
Since it handles only unsigned numbers, do not input any negative numbers. If you have to do so, then opt for string palindrome or change the data types to signed long or integer.
VBA Program to Check Palindrome
After completing the Palindrome program with C, I was just thinking why not do the same in VBA for the most of Excel users. So, here it is – a VBA function that checks whether a string in a Excel cell is Palindrome or not.
Open a new Excel workbook, press Alt + F11 and insert a new module. Copy paste the below code.
Option Explicit 'Palindrome Function in VBA from Officetricks.com Public Function Palindrome(getStr As String) As Boolean Dim iStr As String Dim idx As Integer, ldx As Integer Palindrome = True idx = 1 ldx = VBA.Len(getStr) 'Strip only numbers & albhabets from input string While (idx <= ldx) If (VBA.Mid(getStr, idx, 1) Like "[1-9A-Za-z]") Then iStr = iStr & VBA.UCase(VBA.Mid(getStr, idx, 1)) End If idx = idx + 1 Wend 'Check whetehr reverse of string is also same for Palindrome idx = 1 ldx = VBA.Len(iStr) While (idx < ldx) If (VBA.Mid(iStr, idx, 1) <> VBA.Mid(iStr, ldx, 1)) Then Palindrome = False Exit Function End If idx = idx + 1 ldx = ldx - 1 Wend End Function
Do not execute this code, since it needs a parameter to be passed. Go the Excel worksheet, enter text ‘AbcBa’ or any Palindrome sentence in cell ‘A1’. Now, in cell B1 enter this formula ‘=Palindrome(A1)’.
If the string is a Palindrome, then function will return a ‘TRUE’ else ‘FALSE’.