1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| #include <stdio.h>
#include <string.h>
#define PATTERN_NOT_FOUND -1
int position(char* str_1, char*str_2);
int position(char* str_1, char*str_2)
{
char* ptr_1 = &str_1[0];
char* ptr_2 = strstr(str_1,str_2);
if(ptr_2 == NULL)
{
return PATTERN_NOT_FOUND;
}
else
{
return (int)(ptr_2 - ptr_1);
}
}
int main()
{
char str_1[] = "Hello World!";
char str_2[] = "World";
int pos = position(str_1,str_2);
if(pos != PATTERN_NOT_FOUND)
printf("\"%s\" found at offset %d in \"%s\"\n",str_2,pos,str_1);
else
printf("\"%s\" not found in \"%s\"\n",str_2,str_1);
return 0;
} |