Untitled
unknown
plain_text
2 years ago
1.5 kB
8
Indexable
pragma solidity ^0.8.0; contract PalindromeChecker { function isPalindrome(string memory str) pure public returns (bool) { string memory memoryStr = str; string memory cleanStr = ""; // Remove spaces and punctuation and convert to lowercase for (uint256 i = 0; i < bytes(memoryStr).length; i++) { if (isAlphanumeric(bytes(memoryStr)[i])) { cleanStr = string(abi.encodePacked(cleanStr, toLower(bytes(memoryStr)[i])); } } return isStringPalindrome(cleanStr); } function isAlphanumeric(byte b) pure internal returns (bool) { if ( (b >= byte(uint8('a')) && b <= byte(uint8('z')) || (b >= byte(uint8('A')) && b <= byte(uint8('Z')) || (b >= byte(uint8('0')) && b <= byte(uint8('9')) ) { return true; } return false; } function toLower(byte b) pure internal returns (byte) { if (b >= byte(uint8('A')) && b <= byte(uint8('Z')) { return byte(uint8(b) + 32); } return b; } function isStringPalindrome(string memory str) pure internal returns (bool) { bytes memory bytesStr = bytes(str); uint256 len = bytesStr.length; for (uint256 i = 0; i < len / 2; i++) { if (bytesStr[i] != bytesStr[len - 1 - i]) { return false; } } return true; } }
Editor is loading...