Permalink
Browse files

modified my strstr

  • Loading branch information...
1 parent 9f3e49c commit 3ccb2c4d7a98fb0e1ae82fa638b49a50de9eaab9 @mattknox committed Feb 13, 2012
Showing with 47 additions and 1 deletion.
  1. +47 −1 strstr/strstr.c
View
@@ -1,7 +1,8 @@
+/* before: try to get this done in minimal time and lines */
int strstr(char *orig_needle, char *orig_haystack) {
char *haystack = orig_haystack, *needle = orig_needle;
while(*haystack) {
- if (*needle) {
+ if (*needle == NULL) {
return haystack - orig_haystack;
} else if (*haystack == *needle) {
haystack++;
@@ -13,3 +14,48 @@ int strstr(char *orig_needle, char *orig_haystack) {
}
return -1;
}
+/* after: annoying that I had to do a 3-case comparison */
+
+/* before: gonna do this one without a 3-case comparison */
+int strstr2(char *orig_needle, char *orig_haystack) {
+ char *needle = orig_needle, *haystack = orig_haystack;
+ while (*needle && *haystack) {
+ if (*needle == *haystack) {
+ haystack++;
+ needle++;
+ } else {
+ haystack -= (needle - orig_needle);
+ needle = orig_needle;
+ }
+ }
+ return (*needle ? -1 : haystack - (needle - orig_needle));
+}
+
+int strstr3(char *needle, char *haystack) {
+ int i = 0;
+ while (*haystack && *needle) {
+ if (*(haystack + i) == *(needle + i)) {
+ i++
+ } else {
+ i = 0;
+ haystack++;
+ }
+ }
+ return (i > 0 ? i : -1)
+}
+
+
+int strstr4(char *needle, char *haystack) {
+ int i = 0;
+ while(*haystack) {
+ if (*(needle+i) == NULL) {
+ return i; //wrong.
+ } else if (*(haystack+i) == *(needle+i)) {
+ i++;
+ } else {
+ i = 0;
+ haystack++
+ }
+ }
+ return -1;
+}

0 comments on commit 3ccb2c4

Please sign in to comment.