Skip to content

Instantly share code, notes, and snippets.

@qpfiffer
Created June 19, 2015 23:12
Show Gist options
  • Save qpfiffer/364b90e9681c96f1467a to your computer and use it in GitHub Desktop.
Save qpfiffer/364b90e9681c96f1467a to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// string_compare_guy compares the contents of the left hand side, length with the contents of the
// right hand side, length and returns 1 if the right hand side (needle) is inside the left hand
// side (haystack).
static int string_compare_guy(char* lhs, int llength, char* rhs, int rlength) {
char lnz[llength + 1];
memcpy(lnz, lhs, llength);
lnz[llength] = 0;
char rnz[rlength + 1];
memcpy(rnz, rhs, rlength);
rnz[rlength] = 0;
char *result = strstr(lnz, rhs);
return result == NULL ? 0 : 1;
}
int main() {
int retcode = 0;
printf("Comparing foo and bar\n");
if(string_compare_guy("foo", 3, "bar", 3)) {
retcode++;
printf("failure\n");
} else {
printf("passed\n");
}
printf("trying to find foobar in foo\n");
if(string_compare_guy("foo", 3, "foobar", 6)) {
retcode++;
printf("failure\n");
} else {
printf("passed\n");
}
printf("trying to find foo in foobar\n");
if(string_compare_guy("foobar", 3, "foo", 6)) {
printf("passed\n");
} else {
retcode++;
printf("failure\n");
}
return retcode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment