What's Here?
- Members: 117,572
- Replies: 431,930
- Topics: 66,688
- Snippets: 2,395
- Tutorials: 631
- Total Online: 1,890
- Members: 56
- Guests: 1,834
Who's Online?
|
Welcome to Dream.In.Code |
|
|
Getting C++ Help is Easy!
Join 117,572 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,890 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
|
There are two versions, one which is case sensitive, and another which isn't case sensitive.
|
Submitted By: gabehabe
|
|
Rating:
|
|
Views: 964 |
Language: C++
|
|
Last Modified: July 3, 2008 |
Snippet
/**
* CHECK A STRING TO FIND OUT IF IT IS A PALINDROME
* BY DANNY BATTISON
* gabehabe@hotmail.com
*/
#include <cctype> // tolower()
#include <string>
/* Check if a word is a palindrome (case sensitive!) */
bool isPalindrome (std::string word)
{
for (unsigned int i = 0; i < (word.length()/2); i++)
if (word[i] != word[(word.length()-1)-i])
return false;
/* If this point has been reached, then the word is a palindrome */
return true;
}
/* CASE INSENSITIVE */
bool isPalindromeCI (std::string word)
{
/* Convert our string, character by character */
for (unsigned int i = 0; i < word.length(); i++)
word[i] = tolower(word[i]);
/* Now our word is all lower case, we can pass it to our other function */
return isPalindrome (word);
}
/** EXAMPLE USAGE **/
#include <iostream>
int main ()
{
bool palin = isPalindromeCI ("Hannah");
/* palin is true, since we use our case insensitive version */
bool palin2 = isPalindrome ("Hannah");
/* palin2 is false, since isPalindrome is case sensitive */
std::cin.get (); /* Pause */
return EXIT_SUCCESS; /* Program was executed successfully */
}
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|