code_junkie

How to check if the given string is palindrome?

Nov 10th, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. $string = "A man, a plan, a canal, Panama";
  2.  
  3. function is_palindrome($string)
  4. {
  5. $a = strtolower(preg_replace("/[^A-Za-z0-9]/","",$string));
  6. return $a==strrev($a);
  7. }
  8.  
  9. @echo off
  10.  
  11. call :is_palindrome %1
  12. if %ERRORLEVEL% == 0 (
  13. echo %1 is a palindrome
  14. ) else (
  15. echo %1 is NOT a palindrome
  16. )
  17. exit /B 0
  18.  
  19. :is_palindrome
  20. set word=%~1
  21. set reverse=
  22. call :reverse_chars "%word%"
  23. set return=1
  24. if "$%word%" == "$%reverse%" (
  25. set return=0
  26. )
  27. exit /B %return%
  28.  
  29. :reverse_chars
  30. set chars=%~1
  31. set reverse=%chars:~0,1%%reverse%
  32. set chars=%chars:~1%
  33. if "$%chars%" == "$" (
  34. exit /B 0
  35. ) else (
  36. call :reverse_chars "%chars%"
  37. )
  38. exit /B 0
  39.  
  40. rev = StringReverse(originalString)
  41. return ( rev == originalString );
  42.  
  43. var str = "a b a";
  44. var test = Enumerable.SequenceEqual(str.ToCharArray(),
  45. str.ToCharArray().Reverse());
  46.  
  47. boolean IsPalindrome(string s) {
  48. for (int i = 0; i < s.Length / 2; i++)
  49. {
  50. if (s[i] != s[s.Length - 1 - i]) return false;
  51. }
  52. return true;
  53. }
  54.  
  55. >>> def is_palindrome(s):
  56. ... return s == s[::-1]
  57.  
  58. class String
  59. def palindrome?
  60. (test = gsub(/[^A-Za-z]/, '').downcase) == test.reverse
  61. end
  62. end
  63.  
  64. bool IsPalindrome(char *s)
  65. {
  66. int i,d;
  67. int length = strlen(s);
  68. char cf, cb;
  69.  
  70. for(i=0, d=length-1 ; i < length && d >= 0 ; i++ , d--)
  71. {
  72. while(cf= toupper(s[i]), (cf < 'A' || cf >'Z') && i < length-1)i++;
  73. while(cb= toupper(s[d]), (cb < 'A' || cb >'Z') && d > 0 )d--;
  74. if(cf != cb && cf >= 'A' && cf <= 'Z' && cb >= 'A' && cb <='Z')
  75. return false;
  76. }
  77. return true;
  78. }
  79.  
  80. public static bool IsPalindrome(string palindromeCandidate)
  81. {
  82. if (string.IsNullOrEmpty(palindromeCandidate))
  83. {
  84. return true;
  85. }
  86. Regex nonAlphaChars = new Regex("[^a-z0-9]");
  87. string alphaOnlyCandidate = nonAlphaChars.Replace(palindromeCandidate.ToLower(), "");
  88. if (string.IsNullOrEmpty(alphaOnlyCandidate))
  89. {
  90. return true;
  91. }
  92. int leftIndex = 0;
  93. int rightIndex = alphaOnlyCandidate.Length - 1;
  94. while (rightIndex > leftIndex)
  95. {
  96. if (alphaOnlyCandidate[leftIndex] != alphaOnlyCandidate[rightIndex])
  97. {
  98. return false;
  99. }
  100. leftIndex++;
  101. rightIndex--;
  102. }
  103. return true;
  104. }
  105.  
  106. def IsPalindromeString(n):
  107. myLen = len(n)
  108. i = 0
  109. while i <= myLen/2:
  110. if n[i] != n[myLen-1-i]:
  111. return False
  112. i += 1
  113. return True
  114.  
  115. Delphi
  116.  
  117. function IsPalindrome(const s: string): boolean;
  118. var
  119. i, j: integer;
  120. begin
  121. Result := false;
  122. j := Length(s);
  123. for i := 1 to Length(s) div 2 do begin
  124. if s[i] <> s[j] then
  125. Exit;
  126. Dec(j);
  127. end;
  128. Result := true;
  129. end;
  130.  
  131. public class QuickTest {
  132.  
  133. public static void main(String[] args) {
  134. check("AmanaplanacanalPanama".toLowerCase());
  135. check("Hello World".toLowerCase());
  136. }
  137.  
  138. public static void check(String aString) {
  139. System.out.print(aString + ": ");
  140. char[] chars = aString.toCharArray();
  141. for (int i = 0, j = (chars.length - 1); i < (chars.length / 2); i++, j--) {
  142. if (chars[i] != chars[j]) {
  143. System.out.println("Not a palindrome!");
  144. return;
  145. }
  146. }
  147. System.out.println("Found a palindrome!");
  148. }
  149.  
  150. bool palindrome(std::string const& s)
  151. {
  152. return std::equal(s.begin(), s.end(), s.rbegin());
  153. }
  154.  
  155. #include <string>
  156. #include <iostream>
  157.  
  158. using namespace std;
  159. bool palindrome(string foo)
  160. {
  161. string::iterator front;
  162. string::reverse_iterator back;
  163. bool is_palindrome = true;
  164. for(front = foo.begin(), back = foo.rbegin();
  165. is_palindrome && front!= foo.end() && back != foo.rend();
  166. ++front, ++back
  167. )
  168. {
  169. if(*front != *back)
  170. is_palindrome = false;
  171. }
  172. return is_palindrome;
  173. }
  174. int main()
  175. {
  176. string a = "hi there", b = "laval";
  177.  
  178. cout << "String a: "" << a << "" is " << ((palindrome(a))? "" : "not ") << "a palindrome." <<endl;
  179. cout << "String b: "" << b << "" is " << ((palindrome(b))? "" : "not ") << "a palindrome." <<endl;
  180.  
  181. }
  182.  
  183. private static bool Pal(string s) {
  184. for (int i = 0; i < s.Length; i++) {
  185. if (s[i] != s[s.Length - 1 - i]) {
  186. return false;
  187. }
  188. }
  189. return true;
  190. }
  191.  
  192. bool isPalindromeA(const std::string & p_strText)
  193. {
  194. if(p_strText.length() < 2) return true ;
  195. const char * pStart = p_strText.c_str() ;
  196. const char * pEnd = pStart + p_strText.length() - 1 ;
  197.  
  198. for(; pStart < pEnd; ++pStart, --pEnd)
  199. {
  200. if(*pStart != *pEnd)
  201. {
  202. return false ;
  203. }
  204. }
  205.  
  206. return true ;
  207. }
  208.  
  209. template <typename T>
  210. bool isPalindromeB(const T & p_aText)
  211. {
  212. if(p_aText.empty()) return true ;
  213. typename T::size_type iStart = 0 ;
  214. typename T::size_type iEnd = p_aText.size() - 1 ;
  215.  
  216. for(; iStart < iEnd; ++iStart, --iEnd)
  217. {
  218. if(p_aText[iStart] != p_aText[iEnd])
  219. {
  220. return false ;
  221. }
  222. }
  223.  
  224. return true ;
  225. }
  226.  
  227. template <typename T>
  228. bool isPalindromeC(const T & p_aText)
  229. {
  230. if(p_aText.empty()) return true ;
  231. typename T::const_iterator pStart = p_aText.begin() ;
  232. typename T::const_iterator pEnd = p_aText.end() ;
  233. --pEnd ;
  234.  
  235. while(true)
  236. {
  237. if(*pStart != *pEnd)
  238. {
  239. return false ;
  240. }
  241.  
  242. if((pStart == pEnd) || (++pStart == pEnd))
  243. {
  244. return true ;
  245. }
  246.  
  247. --pEnd ;
  248. }
  249. }
  250.  
  251. boolean isPalindrome(String str1) {
  252. //first strip out punctuation and spaces
  253. String stripped = str1.replaceAll("[^a-zA-Z0-9]", "");
  254. return stripped.equalsIgnoreCase((new StringBuilder(stripped)).reverse().toString());
  255. }
  256.  
  257. static bool isPalindrome(string s)
  258. {
  259. string allowedChars = "abcdefghijklmnopqrstuvwxyz"+
  260. "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  261. string compareString = String.Empty;
  262. string rev = string.Empty;
  263.  
  264. for (int i = 0; i <= s.Length - 1; i++)
  265. {
  266. char c = s[i];
  267.  
  268. if (allowedChars.IndexOf(c) > -1)
  269. {
  270. compareString += c;
  271. }
  272. }
  273.  
  274.  
  275. for (int i = compareString.Length - 1; i >= 0; i--)
  276. {
  277. char c = compareString[i];
  278. rev += c;
  279. }
  280.  
  281. return rev.Equals(compareString,
  282. StringComparison.CurrentCultureIgnoreCase);
  283. }
  284.  
  285. private static boolean doPal(String test) {
  286. for(int i = 0; i < test.length() / 2; i++) {
  287. if(test.charAt(i) != test.charAt(test.length() - 1 - i)) {
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293.  
  294. (defun palindrome(x) (string= x (reverse x)))
  295.  
  296. (defun palindrome(x) (string= x (reverse x)))
  297.  
  298. import string
  299.  
  300. def is_palindrome(palindrome):
  301. letters = palindrome.translate(string.maketrans("",""),
  302. string.whitespace + string.punctuation).lower()
  303. return letters == letters[::-1]
  304.  
  305. std::string a = "god";
  306. std::string b = "lol";
  307.  
  308. std::cout << (std::string(a.rbegin(), a.rend()) == a) << " "
  309. << (std::string(b.rbegin(), b.rend()) == b);
  310.  
  311. function ispalin { [ "$( echo -n $1 | tac -rs . )" = "$1" ]; }
  312. echo "$(ispalin god && echo yes || echo no), $(ispalin lol && echo yes || echo no)"
  313.  
  314. /* obvious solution */
  315. function ispalin(cand, i) {
  316. for(i=0; i<length(cand)/2; i++)
  317. if(substr(cand, length(cand)-i, 1) != substr(cand, i+1, 1))
  318. return 0;
  319. return 1;
  320. }
  321.  
  322. /* not so obvious solution. cough cough */
  323. {
  324. orig = $0;
  325. while($0) {
  326. stuff = stuff gensub(/^.*(.)$/, "\1", 1);
  327. $0 = gensub(/^(.*).$/, "\1", 1);
  328. }
  329. print (stuff == orig);
  330. }
  331.  
  332. ispalin :: [Char] -> Bool
  333. ispalin a = a == (let xi (y:my) = (xi my) ++ [y]; xi [] = [] in x -> xi x) a
  334.  
  335. int IsPalindrome (char *s)
  336. {
  337. char*a,*b,c=0;
  338. 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));
  339. return s!=0;
  340. }
  341.  
  342. string mystring = "abracadabra";
  343.  
  344. char[] str = mystring.ToCharArray();
  345. Array.Reverse(str);
  346. string revstring = new string(str);
  347.  
  348. if (mystring.equals(revstring))
  349. {
  350. Console.WriteLine("String is a Palindrome");
  351. }
  352.  
  353. public boolean isPalindrome(String phrase) {
  354. phrase = phrase.toLowerCase().replaceAll("[^a-z]", "");
  355. return StringUtils.reverse(phrase).equals(phrase);
  356. }
  357.  
  358. isPalindrome :: String -> Bool
  359. isPalindrome n = (n == reverse n)
  360.  
  361. if s == s[::-1]: return True
  362.  
  363. if (s.Equals(s.Reverse())) { return true; }
  364.  
  365. if (s == strrev(s)) return true;
  366.  
  367. if (s == reverse(s)) { return true; }
  368.  
  369. string:equal(S, lists:reverse(S)).
Add Comment
Please, Sign In to add comment