Skip to content

Instantly share code, notes, and snippets.

@DreamVB
Created September 4, 2016 17:49
Show Gist options
  • Select an option

  • Save DreamVB/b989e9caa9c2f610f5872f3651debf63 to your computer and use it in GitHub Desktop.

Select an option

Save DreamVB/b989e9caa9c2f610f5872f3651debf63 to your computer and use it in GitHub Desktop.
Check if a word is Palindrome the word should read the same as backwaords or forwards.
// Check if a word is Palindrome
// The word sould read the same as backwaords or forwards.
// I used used code to lowercase string as to avoid captital letters.
//By DreamVB
#include <iostream>
#include <Windows.h>
using namespace std;
using std::cout;
using std::endl;
bool IsPalindrome(char *s){
char temp[80];
int x = 0;
int len = strlen(s);
int y = (len - 1);
bool IsGood = true;
//Copy src into temp
strcpy(temp, s);
//Convert to lowercase
while (x < len){ temp[x] = tolower(temp[x]); x++; }
x = 0;
while (x < y){
//Test start and end of temp
if (temp[x] != temp[y]){
//No good exit
IsGood = false;
break;
}
//INC/DEC counters
x++;
y--;
}
//Return result.
return IsGood;
}
int main(int argc, char **anvg){
cout << IsPalindrome("AMANAPLANACANALPANAMA") << endl; // Returns true
cout << IsPalindrome("RACECAR") << endl; // Return true
cout << IsPalindrome("Test") << endl; // Return false
//Keep console open
system("pause");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment