Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #define STAR_CHR '*'
- int
- mysubstr(char *needle, char *haystack)
- {
- char *star = strchr(needle, STAR_CHR);
- if (star != NULL) {
- // separate two parts of the string
- *star = 0;
- // now needle is he first part
- // and star + 1 is the second part
- }
- char *res = strstr(haystack, needle);
- // NOTE: strstr uses reverse args order
- if (star == NULL) {
- // if there was no star, we're done
- return res == NULL ? -1 : res - haystack;
- }
- if (res == NULL) {
- *star = STAR_CHR;
- return -1;
- }
- // search for the second part of the needle
- // in the remaining part of the haystack
- char *second_part = strstr(res + strlen(needle), star + 1);
- *star = STAR_CHR;
- return second_part == NULL ? -1 : res - haystack;
- }
- #ifdef DEBUG
- #include <stdio.h>
- int main(void) {
- char needle[80];
- char haystack[80];
- scanf("%s%s", needle, haystack);
- printf("%d\n", mysubstr(needle, haystack));
- return 0;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement