-
Feb 2nd, 2015, 12:32 PM
#1
Palindrome Checker
It amazes me how many people need to write a function that checks if a word is a palindrome(same forwards as it is backwards). Here is a function that checks if a word or phrase is a palindrome.
The function allows for the user to ignore both the white space and casing.
Code:
''' <summary>
''' Checks if a word is the same forward as it is backwards
''' </summary>
''' <param name="value">The value to check.</param>
''' <param name="ignoreWhiteSpace">Determines if whitespace in the value should be ignored.</param>
''' <param name="ignoreCasing">Determines if the casing of the value should be ignored.</param>
''' <returns>Boolean</returns>
Private Function IsPalindrome(ByVal value As String, ByVal ignoreWhiteSpace As Boolean, ByVal ignoreCasing As Boolean) As Boolean
Dim comparison As StringComparison = If(ignoreCasing, StringComparison.CurrentCultureIgnoreCase, StringComparison.CurrentCulture)
If ignoreWhiteSpace Then
Return String.Equals(value.Replace(" ", String.Empty), String.Join("", value.Replace(" ", String.Empty).ToArray.Reverse), comparison)
Else
Return String.Equals(value, String.Join("", value.ToArray.Reverse), comparison)
End If
End Function
-
Apr 11th, 2015, 09:32 PM
#2
Junior Member
Re: Palindrome Checker
"A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. Allowances may be made for adjustments to capital letters, punctuation, and word dividers." - Google
http://en.wikipedia.org/wiki/Palindrome
those people looking for that are usually doing a homework assignment. Dont think your code takes the accepted definition into consideration. every famous palindrome would fail with your code.
professional palendromist actually start from the center and work outwards.. this i learned from reading an article about the person who has created the worlds longest palindrome. and he mentions others who actually use this method as well.
here is some c++ i had did a few years ago to help such a person with their homework assignment (this does not start from the center as it should. i did this before i found out about that):
Code:
// palindrome.cpp : console application.
//Created By: Heriberto Lugo
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool checkPalindrome(string);
string alphaString(string);
string lowerIt(string);
int main()
{
bool isPalindrome = false;
string word = "";
char again = 'y';
string alphaWord = "";
while (again == 'y' || again == 'Y') {
cout << endl << endl;
cout << "Type a word and press enter to see if it is a Palindrome." << endl;
getline(cin, word);
alphaWord = alphaString(word);
alphaWord = lowerIt(alphaWord);
isPalindrome = checkPalindrome(alphaWord);
switch (isPalindrome) {
case true:
cout << word << " is a Palindrome!" << endl << endl;
break;
case false:
cout << word << " is not a Palindrome." << endl << endl;
break;
}
/*cout << "type [Y] and hit enter to try another word." << endl;
cin >> again;
cin.ignore();*/
}
return 0;
}
bool checkPalindrome(string word){
size_t checkPoint = word.size() / 2;
size_t currentPoint = 0;
while (currentPoint != checkPoint){
size_t endPoint = (word.size() - 1) - currentPoint;
if (word[currentPoint] != word[endPoint]) {
return false;
}
currentPoint++;
}
return true;
}
string alphaString(string input){
string newInput = "";
int size;
int i = 0;
int newI = 0;
size = input.size();
do {
if (isalpha(input[i])){
newInput += input[i];
newI++;
}
i++;
} while (i <= size - 1);
return newInput;
}
string lowerIt(string word) {
char c;
std::vector<char> str (word.begin(), word.end() );
string newWord = "";
for (std::vector<char>::iterator it = str.begin(); it != str.end(); ++it){
c= *it;
newWord += (tolower(c));
}
return newWord;
}
here it is with a gui (some mods made to code at the point i put it in a gui)
the highlighter has an occasional bug
here is a downloadable if you want to see how it works..
http://1drv.ms/ZoRIpq
-
Apr 13th, 2015, 12:47 PM
#3
Re: Palindrome Checker
I don't know how my example would fail. I used this example it returned the correct value each time:
Code:
Public Sub Main()
Dim word As String = "RaCecar"
Dim phrase As String = "Too hot to hoot"
Dim number As String = "12321"
Console.WriteLine(IsPalindrome(word, True, True)) 'True because of ignore-case
Console.WriteLine(IsPalindrome(word, True, False)) 'False because of capital R and C
Console.WriteLine(IsPalindrome(phrase, True, True)) 'True because of ignore whitespace and ignore-case
Console.WriteLine(IsPalindrome(phrase, False, True)) 'False because of two whitespace between hot and to
Console.WriteLine(IsPalindrome(phrase, True, False)) 'False because of Capital T
Console.WriteLine(IsPalindrome(number, True, True)) 'True because there is no whitespace or casing in words
End Sub
-
Apr 16th, 2015, 08:28 PM
#4
Junior Member
Re: Palindrome Checker
Please review the accepted definition for what a palindrome is. it is not simply a phrase, word or sentence in which characters are compared from front to end.
Test this in your code:
A dog, a plan, a canal: pagoda.
Notice, it will fail at the period. then at each comma, then at the semi colon.
So your code does not test an actual Palindrome.
Thats really all its missing..
Most codes i see fail at capitals and whitespace..
-
Apr 16th, 2015, 09:33 PM
#5
Re: Palindrome Checker
OK, I understand now. So in order for it to be a "true" palindrome checker it must account for grammar as well? If that is the case then I'll create another function which allows to ignore grammar.
-
Dec 31st, 2015, 04:02 AM
#6
New Member
Re: Palindrome Checker
My approach, simple as this:
Code:
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Determines whether the specified string is palindrome.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <remarks>
''' A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.
''' <para></para>
''' Allowances may be made for adjustments to capital letters, punctuation, and word dividers.
''' <para></para>
''' For more info, see Wikipedia: <see href="https://en.wikipedia.org/wiki/Palindrome"/>
''' </remarks>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Dim isPalindrome As Boolean = StringIsPalindrome("A man, a plan, a canal, Panama!")
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <param name="str">
''' The string to examine.
''' </param>
''' ----------------------------------------------------------------------------------------------------
''' <returns>
''' <see langword="True"/> if the specified string is palindrome; otherwise, <see langword="False"/>.
''' </returns>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Shared Function StringIsPalindrome(ByVal str As String) As Boolean
Dim tmpStr As String =
String.Join("", (From c As Char In str
Where Not Char.IsPunctuation(c) AndAlso
Not Char.IsWhiteSpace(c)))
Return String.Equals(tmpStr, String.Join("", tmpStr.Reverse),
StringComparison.OrdinalIgnoreCase)
End Function
If you like this code, you can find more of my string utilities here:
Moderator Action: -Removed Link Due to Advertising-
Thanks for read.
Last edited by dday9; Dec 31st, 2015 at 09:10 AM.
-
Dec 31st, 2015, 09:13 AM
#7
Re: Palindrome Checker
That looks very similar to my example only you removed punctuation and whitespace where as I give the option to ignore whitespace. Also I removed your link because you were clearly advertising.
-
Dec 31st, 2015, 09:53 AM
#8
New Member
Re: Palindrome Checker
Originally Posted by dday9
That looks very similar to my example only you removed punctuation and whitespace where as I give the option to ignore whitespace.
Yes, your implementation looks very similar to my example, only you didn't removed punctuations and you gave an unnecessary parameter to optionally ignore what shouldn't be ignored.
Whats the problem?, what are you trying to argument?, or just you can't accept a refactor or improvement with good education...? ( incredible from a moderator )
Thanks for read.
Last edited by pitoloko; Dec 31st, 2015 at 09:58 AM.
-
Dec 31st, 2015, 09:58 AM
#9
Re: Palindrome Checker
Yes, your implementation looks very similar to my example, only you didn't removed punctuations and you gave an unnecessary parameter to optionally ignore what shouldn't be ignored.
The whitespace parameter isn't unnecessary, in some instances you need to include the whitespace. However I did not consider giving the option to ignore punctuation.
Whats the problem?, what are you trying to argument?, or just you can't accept a refactor or improvement with good education...? ( incredible from a moderator )
What are you talking about? I'm not trying to argue with you and I never rejected your code. I only stated that it's very similar to mine. I only removed your link because it violated the acceptable use policy which you agreed to follow when you became a member of VBForums. As a moderator it is my responsibility to enforce the rules.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|