siggyjav
asked on
finding specific char substring
I need a function to search for specific text in a string.
I tried:
if (instr(line, "hello") != 0 ) /* line is a string of type char */
but I keep getting an unresolved error at compile.
If I am doing something wrong with this function...please let me know.
If you could also provide other means of doing this it would be much appreciated.
Thanks
I tried:
if (instr(line, "hello") != 0 ) /* line is a string of type char */
but I keep getting an unresolved error at compile.
If I am doing something wrong with this function...please let me know.
If you could also provide other means of doing this it would be much appreciated.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the documentation for instr() tells its a curses function, used for extracting string of characters
starting from the current cursor position in a named window. i think you are using the wrong
function, the resolve error comes because you arent linking in the curses library.
starting from the current cursor position in a named window. i think you are using the wrong
function, the resolve error comes because you arent linking in the curses library.
errata:
int the definition of getindex we should have ..
int getindex(line, mystring); //mistakenly typed string earlier
char *line;
char *mystring;
{
......
int the definition of getindex we should have ..
int getindex(line, mystring); //mistakenly typed string earlier
char *line;
char *mystring;
{
......
#include <string.h>
#include <stdio.h>
int getindex(line, string)
char *line;
char *mystring;
{
int i;
int slen = strlen(mystring);
int llen = strlen(line);
for(i = 0; i < llen; i++){
if(!strncmp(line + i, mystring, slen))
return i;
}
return -1;
}
int main()
{
char *line = "My name is XYZ";
char *string = "XYZ";
printf("%d\n", getindex(line, string));
return 0;
}
getindex returns the index in the line string, where the string mystring appears first.
hope this helps,
van_dy