// Palindrome.C : A palindrome is a string that is spelled the same way forward and backward.
// This code verifies the string entered IGNORING CAPITALIZATIONS, SPACES and PUNCTUATIONS.
// Most of the CRT library functions used are with security enhancements optimized for MS Visual C++ [2010].
// For convenience, remove the "/*.*/" during coding. Same with others, I'm using this to emphasize spacing.
// Removing this does not affect the flow of the program (since /**/ is designed for comment only.)
#include <ctype.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 0x80 // hex for 128.
int main(void)
{
/**/ char *strsrc=(char*)calloc(SIZE,sizeof(char)),*strver=(char*)calloc(SIZE,sizeof(char));
/**/ register size_t count,counter; bool ver;
/**/ printf_s("Enter String: "); gets_s(strsrc,SIZE);
/**/ for(count=0,counter=0;count<strlen(strsrc)+1;count++)
/*.....*/ isspace(*(strsrc+count))ispunct(*(strsrc+count))?true:*(strver+counter++)=tolower(*(strsrc+count));
/**/ for(count=0,counter=strlen(strver)-1;count<strlen(strver);count++,counter--)
/*.....*/ *(strver+counter)==*(strver+count)?ver=true:ver=false;
/**/ printf_s("%s %s a palindrome.",strsrc,ver?"is":"is not");
/**/ _getch(); free(strsrc); free(strver); return 0;
}
// It is suggested to use MS Visual C++. Otherwise, remove "_s" in function names and comply with the
// appropriate passing parameters and other codings for other compilers.
Nothing.
palindrome in every language means same. it means on reversing a number or string, if we get the same number or string as the case may be ,then the number or string is called palindrome. eg: 1221,111,252 or LIRIL,MADAM etc .
/*To check whether a string is palindrome*/includeincludevoid main () { int i,j,f=0; char a[10]; clrscr (); gets(a); for (i=0;a[i]!='\0';i++) { } i--; for (j=0;a[j]!='\0';j++,i--) { if (a[i]!=a[j]) f=1; } if (f==0) printf("string is palindrome"); else printf("string is not palindrome"); getch (); }
The arrangement of characters from left to right in the string is the same as the arrangement of characters from right to left. e.g. MOM is a palindrome as it is same even if you read it from either left to right or right to left. while CAT is not a palindrome.
what ever you assume
Reverse the string and compare it to the original. If they match, then it is a palindrome.
Here is a simple C program to check if a string is a palindrome: #include <stdio.h> #include <string.h> int main() { char str[100], rev[100]; printf("Enter a string: "); fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = 0; // Remove newline character strcpy(rev, str); strrev(rev); // Reverse the string if (strcmp(str, rev) == 0) printf("The string is a palindrome.\n"); else printf("The string is not a palindrome.\n"); return 0; } This program takes a string input, reverses it, and then compares the original string with the reversed string to determine if it is a palindrome.
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char a[15],b[15]; printf("Enter the string\n"); scanf("%s",&a); strcpy(b,a); strrev(a); if(strcmp(a,b)==0) printf("The String is a palindrome"); else printf("The String is not a palindrome"); }
a string constant
len=0 i=1 echo -n "Enter a String: " read str len=`echo $str | wc -c` len=`expr $len - 1` halfLen=`expr $len / 2` while [ $i -le $halfLen ] do c1=`echo $str|cut -c$i` c2=`echo $str|cut -c$len` if [ $c1 != $c2 ] ; then echo "string is not palindrome" exit fi i=`expr $i + 1` len=`expr $len - 1` done echo "String is Palindrome"
len=0i=1echo -n "Enter a String: "read strlen=`echo $str | wc -c`len=`expr $len - 1`halfLen=`expr $len / 2`while [ $i -le $halfLen ]doc1=`echo $str|cut -c$i`c2=`echo $str|cut -c$len`if [ $c1 != $c2 ] ; thenecho "string is not palindrome"exitfii=`expr $i + 1`len=`expr $len - 1`doneecho "String is Palindrome"
Copy and reverse the string. If the reversed string is equal to the original string, the string is a palindrome, otherwise it is not. When working with strings that hold natural language phrases (including punctuation, whitespace and so on) we must remove all the non-alphanumerics and convert the remainder to a common case, such as lower-case, prior to copying and reversing the string.