Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $string = "A man, a plan, a canal, Panama";
- function is_palindrome($string)
- {
- $a = strtolower(preg_replace("/[^A-Za-z0-9]/","",$string));
- return $a==strrev($a);
- }
- @echo off
- call :is_palindrome %1
- if %ERRORLEVEL% == 0 (
- echo %1 is a palindrome
- ) else (
- echo %1 is NOT a palindrome
- )
- exit /B 0
- :is_palindrome
- set word=%~1
- set reverse=
- call :reverse_chars "%word%"
- set return=1
- if "$%word%" == "$%reverse%" (
- set return=0
- )
- exit /B %return%
- :reverse_chars
- set chars=%~1
- set reverse=%chars:~0,1%%reverse%
- set chars=%chars:~1%
- if "$%chars%" == "$" (
- exit /B 0
- ) else (
- call :reverse_chars "%chars%"
- )
- exit /B 0
- rev = StringReverse(originalString)
- return ( rev == originalString );
- var str = "a b a";
- var test = Enumerable.SequenceEqual(str.ToCharArray(),
- str.ToCharArray().Reverse());
- boolean IsPalindrome(string s) {
- for (int i = 0; i < s.Length / 2; i++)
- {
- if (s[i] != s[s.Length - 1 - i]) return false;
- }
- return true;
- }
- >>> def is_palindrome(s):
- ... return s == s[::-1]
- class String
- def palindrome?
- (test = gsub(/[^A-Za-z]/, '').downcase) == test.reverse
- end
- end
- bool IsPalindrome(char *s)
- {
- int i,d;
- int length = strlen(s);
- char cf, cb;
- for(i=0, d=length-1 ; i < length && d >= 0 ; i++ , d--)
- {
- while(cf= toupper(s[i]), (cf < 'A' || cf >'Z') && i < length-1)i++;
- while(cb= toupper(s[d]), (cb < 'A' || cb >'Z') && d > 0 )d--;
- if(cf != cb && cf >= 'A' && cf <= 'Z' && cb >= 'A' && cb <='Z')
- return false;
- }
- return true;
- }
- public static bool IsPalindrome(string palindromeCandidate)
- {
- if (string.IsNullOrEmpty(palindromeCandidate))
- {
- return true;
- }
- Regex nonAlphaChars = new Regex("[^a-z0-9]");
- string alphaOnlyCandidate = nonAlphaChars.Replace(palindromeCandidate.ToLower(), "");
- if (string.IsNullOrEmpty(alphaOnlyCandidate))
- {
- return true;
- }
- int leftIndex = 0;
- int rightIndex = alphaOnlyCandidate.Length - 1;
- while (rightIndex > leftIndex)
- {
- if (alphaOnlyCandidate[leftIndex] != alphaOnlyCandidate[rightIndex])
- {
- return false;
- }
- leftIndex++;
- rightIndex--;
- }
- return true;
- }
- def IsPalindromeString(n):
- myLen = len(n)
- i = 0
- while i <= myLen/2:
- if n[i] != n[myLen-1-i]:
- return False
- i += 1
- return True
- Delphi
- function IsPalindrome(const s: string): boolean;
- var
- i, j: integer;
- begin
- Result := false;
- j := Length(s);
- for i := 1 to Length(s) div 2 do begin
- if s[i] <> s[j] then
- Exit;
- Dec(j);
- end;
- Result := true;
- end;
- public class QuickTest {
- public static void main(String[] args) {
- check("AmanaplanacanalPanama".toLowerCase());
- check("Hello World".toLowerCase());
- }
- public static void check(String aString) {
- System.out.print(aString + ": ");
- char[] chars = aString.toCharArray();
- for (int i = 0, j = (chars.length - 1); i < (chars.length / 2); i++, j--) {
- if (chars[i] != chars[j]) {
- System.out.println("Not a palindrome!");
- return;
- }
- }
- System.out.println("Found a palindrome!");
- }
- bool palindrome(std::string const& s)
- {
- return std::equal(s.begin(), s.end(), s.rbegin());
- }
- #include <string>
- #include <iostream>
- using namespace std;
- bool palindrome(string foo)
- {
- string::iterator front;
- string::reverse_iterator back;
- bool is_palindrome = true;
- for(front = foo.begin(), back = foo.rbegin();
- is_palindrome && front!= foo.end() && back != foo.rend();
- ++front, ++back
- )
- {
- if(*front != *back)
- is_palindrome = false;
- }
- return is_palindrome;
- }
- int main()
- {
- string a = "hi there", b = "laval";
- cout << "String a: "" << a << "" is " << ((palindrome(a))? "" : "not ") << "a palindrome." <<endl;
- cout << "String b: "" << b << "" is " << ((palindrome(b))? "" : "not ") << "a palindrome." <<endl;
- }
- private static bool Pal(string s) {
- for (int i = 0; i < s.Length; i++) {
- if (s[i] != s[s.Length - 1 - i]) {
- return false;
- }
- }
- return true;
- }
- bool isPalindromeA(const std::string & p_strText)
- {
- if(p_strText.length() < 2) return true ;
- const char * pStart = p_strText.c_str() ;
- const char * pEnd = pStart + p_strText.length() - 1 ;
- for(; pStart < pEnd; ++pStart, --pEnd)
- {
- if(*pStart != *pEnd)
- {
- return false ;
- }
- }
- return true ;
- }
- template <typename T>
- bool isPalindromeB(const T & p_aText)
- {
- if(p_aText.empty()) return true ;
- typename T::size_type iStart = 0 ;
- typename T::size_type iEnd = p_aText.size() - 1 ;
- for(; iStart < iEnd; ++iStart, --iEnd)
- {
- if(p_aText[iStart] != p_aText[iEnd])
- {
- return false ;
- }
- }
- return true ;
- }
- template <typename T>
- bool isPalindromeC(const T & p_aText)
- {
- if(p_aText.empty()) return true ;
- typename T::const_iterator pStart = p_aText.begin() ;
- typename T::const_iterator pEnd = p_aText.end() ;
- --pEnd ;
- while(true)
- {
- if(*pStart != *pEnd)
- {
- return false ;
- }
- if((pStart == pEnd) || (++pStart == pEnd))
- {
- return true ;
- }
- --pEnd ;
- }
- }
- boolean isPalindrome(String str1) {
- //first strip out punctuation and spaces
- String stripped = str1.replaceAll("[^a-zA-Z0-9]", "");
- return stripped.equalsIgnoreCase((new StringBuilder(stripped)).reverse().toString());
- }
- static bool isPalindrome(string s)
- {
- string allowedChars = "abcdefghijklmnopqrstuvwxyz"+
- "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string compareString = String.Empty;
- string rev = string.Empty;
- for (int i = 0; i <= s.Length - 1; i++)
- {
- char c = s[i];
- if (allowedChars.IndexOf(c) > -1)
- {
- compareString += c;
- }
- }
- for (int i = compareString.Length - 1; i >= 0; i--)
- {
- char c = compareString[i];
- rev += c;
- }
- return rev.Equals(compareString,
- StringComparison.CurrentCultureIgnoreCase);
- }
- private static boolean doPal(String test) {
- for(int i = 0; i < test.length() / 2; i++) {
- if(test.charAt(i) != test.charAt(test.length() - 1 - i)) {
- return false;
- }
- }
- return true;
- }
- (defun palindrome(x) (string= x (reverse x)))
- (defun palindrome(x) (string= x (reverse x)))
- import string
- def is_palindrome(palindrome):
- letters = palindrome.translate(string.maketrans("",""),
- string.whitespace + string.punctuation).lower()
- return letters == letters[::-1]
- std::string a = "god";
- std::string b = "lol";
- std::cout << (std::string(a.rbegin(), a.rend()) == a) << " "
- << (std::string(b.rbegin(), b.rend()) == b);
- function ispalin { [ "$( echo -n $1 | tac -rs . )" = "$1" ]; }
- echo "$(ispalin god && echo yes || echo no), $(ispalin lol && echo yes || echo no)"
- /* obvious solution */
- function ispalin(cand, i) {
- for(i=0; i<length(cand)/2; i++)
- if(substr(cand, length(cand)-i, 1) != substr(cand, i+1, 1))
- return 0;
- return 1;
- }
- /* not so obvious solution. cough cough */
- {
- orig = $0;
- while($0) {
- stuff = stuff gensub(/^.*(.)$/, "\1", 1);
- $0 = gensub(/^(.*).$/, "\1", 1);
- }
- print (stuff == orig);
- }
- ispalin :: [Char] -> Bool
- ispalin a = a == (let xi (y:my) = (xi my) ++ [y]; xi [] = [] in x -> xi x) a
- int IsPalindrome (char *s)
- {
- char*a,*b,c=0;
- for(a=b=s;a<=b;c=(c?c==1?c=(*a&~32)-65>25u?*++a,1:2:c==2?(*--b&~32)-65<26u?3:2:c==3?(*b-65&~32)-(*a-65&~32)?*(b=s=0,a),4:*++a,1:0:*++b?0:1));
- return s!=0;
- }
- string mystring = "abracadabra";
- char[] str = mystring.ToCharArray();
- Array.Reverse(str);
- string revstring = new string(str);
- if (mystring.equals(revstring))
- {
- Console.WriteLine("String is a Palindrome");
- }
- public boolean isPalindrome(String phrase) {
- phrase = phrase.toLowerCase().replaceAll("[^a-z]", "");
- return StringUtils.reverse(phrase).equals(phrase);
- }
- isPalindrome :: String -> Bool
- isPalindrome n = (n == reverse n)
- if s == s[::-1]: return True
- if (s.Equals(s.Reverse())) { return true; }
- if (s == strrev(s)) return true;
- if (s == reverse(s)) { return true; }
- string:equal(S, lists:reverse(S)).
Add Comment
Please, Sign In to add comment