Following string palindrome check function in c++ without using string.h works with the constraints :
- strings that do not contain any space
- strings just contain English alphabet characters
- strings with more than 3 characters
#include <stdio.h> int isPalindrome( const char* arr ) { int stringLength = 0; int index = 0; while(arr[index]!='\0') { ++stringLength; ++index; } if( stringLength >= 0 && stringLength <=2 ) return 0; for(int i = 0; i<stringLength/2; i++) { if(arr[i] != arr[stringLength-1-i]) return 0; } return 1; } int main() { const char* stringToCheck1 = "aba"; int res = isPalindrome(stringToCheck1); if(res) printf("%s is palindrome\n", stringToCheck1); else printf("%s is not palindrome\n",stringToCheck1); const char* stringToCheck2 = "abba"; res = isPalindrome(stringToCheck2); if(res) printf("%s is palindrome\n",stringToCheck2); else printf("%s is not palindrome\n",stringToCheck2); const char* stringToCheck3 = "abccccba"; res = isPalindrome(stringToCheck3); if(res) printf("%s is palindrome\n",stringToCheck3); else printf("%s is not palindrome\n",stringToCheck3); const char* stringToCheck4 = "abcc111211ccba"; res = isPalindrome(stringToCheck4); if(res) printf("%s is palindrome\n",stringToCheck4); else printf("%s is not palindrome\n",stringToCheck4); const char* stringToCheck5 = ""; res = isPalindrome(stringToCheck5); if(res) printf("%s is palindrome\n",stringToCheck5); else printf("%s is not palindrome\n",stringToCheck5); return 0; }After executing above main function following terminal output is produced.
No comments:
Post a Comment