Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created March 13, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzarcon/fbdadb6391a188c9c85b to your computer and use it in GitHub Desktop.
Save zzarcon/fbdadb6391a188c9c85b to your computer and use it in GitHub Desktop.
Palindrome C++
#include <nan.h>
using namespace v8;
void IsPalindrome(const FunctionCallbackInfo<Value>& info) {
String::Utf8Value sentence(info[0]->ToString());
std::string str = std::string(*sentence);
int len = str.length();
int half = len / 2;
int start = 0;
int end = len - 1;
int space = 32;
bool isPal = true;
while (half > 0 && isPal) {
bool startSpace = str.at(start) == space;
bool endSpace = str.at(end) == space;
if (str.at(start) == str.at(end)) {
start++;
end--;
} else if (startSpace || endSpace) {
startSpace && start++;
endSpace && end--;
} else {
isPal = false;
}
half--;
}
info.GetReturnValue().Set(isPal);
}
void Init(Local<Object> exports, Local<Object> module) {
NODE_SET_METHOD(module, "exports", IsPalindrome);
}
NODE_MODULE(addon, Init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment