I am really new to C.
I want to use the strpos function but it is telling me it doesnt exist?
Here a complete snippet code to solve you problem. PS: Isn't too late to help. ;)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NOT_FOUND -1
int main (){
int pos = NOT_FOUND;
if ( (pos = strpos( "subsstring", "string")) != NOT_FOUND )
printf("found at %d\n", pos);
else
printf("not found!\n");
return 0;
}
int strpos(char *haystack, char *needle)
{
char *p = strstr(haystack, needle);
if (p)
return p - haystack;
return NOT_FOUND;
}
Edit: Answering Can Vural question:
No. I really think that it would be as it is. At structured programming paradigm, it's a common practice to use the scope structure as first parameter on every function that belongs to the structure's scope itself. The strstr function defined at string.h follow the same approach.
On OOP you have haystack.indexOf( needle )
. At structured programming, you have indexOf( haystack, needle )
.
The function you are looking for might be either strstr or strchr. You then need to include string.h. There is no strpos in the POSIX interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With