Skip to content

Instantly share code, notes, and snippets.

@zzarcon
Created March 13, 2016 15:47
Show Gist options
  • Select an option

  • Save zzarcon/f7e5434d605c1b12b6d9 to your computer and use it in GitHub Desktop.

Select an option

Save zzarcon/f7e5434d605c1b12b6d9 to your computer and use it in GitHub Desktop.
Palindrome C++ 2
#include <nan.h>
using namespace v8;
void IsPalindrome(const FunctionCallbackInfo<Value>& info) {
Nan::Utf8String arg0(info[0]);
char *str = *arg0;
size_t len = arg0.length();
int half = len / 2;
int start = 0;
int end = len - 1;
int space = 32;
int comma = 44;
bool isPal = true;
bool startSpace;
bool endSpace;
while (half > 0 && isPal) {
startSpace = str[start] == space || str[start] == comma;
endSpace = str[end] == space || str[end] == comma;
if (str[start] == str[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