ccplusplus.com
Learn C, C++ Concepts
Monday, September 26, 2011
c program to check string is palindrome
/*********************************************************************** * File : check-string-is-palindrome.c * Author : Saurabh Gupta * Desc : palindrome check in c * program to check string is palindrome or not in c * program to check whether a string is palindrome or not * palindrome or not in c * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : AM 07:27 26 September 2011 * Note : try to modify the code by removing the gets ************************************************************************/ #include <stdio.h> #include <string.h> #include <ctype.h> int main () { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXXXXX palindrome checker XXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); char caUserInput[500]; // Stores the word to be tested char caUserInputInChars[500]; // Stores the word without punctuation and spaces size_t j = 0; // Index to character position size_t i = 0; size_t length = 0; // Length of a string printf("Enter a word to be tested:\n"); gets (caUserInput); /* * Copy only letters as lowercase */ for ( ; i< strlen(caUserInput) ; i++) { if(isalpha(caUserInput[i])) { caUserInputInChars[j++] = tolower(caUserInput[i]); } } caUserInputInChars[j] = '\0'; // Append string terminator length = strlen(caUserInputInChars); // Get the satring length /* * Compare matching characters in the string * If any pair are not the same, then it's not a palindrome */ for(i = 0 ; i<=length/2 ; i++) { if(caUserInputInChars[i] != caUserInputInChars[length-1-i]) { printf("'%s' is not a palindrome.\n", caUserInput); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } } /* If we arrive here all matching pairs of characters are equal */ printf("'%s' is a palindrome.\n", caUserInput); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } /* * * [sgupta@rhel54x64 c]$ gcc check-string-is-palindrome.c -o check-string-is-palindrome /tmp/ccoR1Mya.o: In function `main': check-string-is-palindrome.c:(.text+0x54): warning: the `gets' function is dangerous and should not be used. [sgupta@rhel54x64 c]$ ./check-string-is-palindrome XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX palindrome checker XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter a word to be tested: anna 'anna' is a palindrome. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ ./check-string-is-palindrome XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXX palindrome checker XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter a word to be tested: saurabh 'saurabh' is not a palindrome. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment