Skip to content

Instantly share code, notes, and snippets.

@johno
Created March 15, 2014 17:57
Show Gist options
  • Save johno/9571233 to your computer and use it in GitHub Desktop.
Save johno/9571233 to your computer and use it in GitHub Desktop.
#include "mystrstr.h"
#include <string.h>
#include <stdio.h>
char big_string[] = "Hello";
char sub_string[] = "ell";
char *str = big_string;
char *substr = sub_string;
char* mystrstr(char *s, char *subs) {
char *a = s;
char *b = subs;
for( ; *s != '\0'; *s++) {
if(*s != *b) {
continue;
}
a = s;
while(1) {
if(*b == '\0') {
return s;
} else if(*a++ != *b++) {
break;
}
}
b = subs;
}
return (char *) NULL;
}
int main(void) {
char *res = mystrstr(str, substr);
if(res != (char *) NULL) {
printf("Substring match starting at: %c\n", *mystrstr(str, substr));
} else {
printf("There was no match found.\n");
}
return 1;
}
#define NULL -1
char* mystrstr(char *s, char *subs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment