Solved

finding specific char substring

Posted on 2004-10-22
196 Views
Last Modified: 2010-08-05
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

 
0
Question by:siggyjav
    4 Comments
     
    LVL 86

    Accepted Solution

    by:
    Use 'strstr()' instead:

    #include <string.h>

    if (strstr(line, "hello") != 0 )    /* line is a string of type char */

    Alittle more complex example would be

    /* STRSTR.C */

    #include <string.h>
    #include <stdio.h>

    char str[] =    "lazy";
    char string[] = "The quick brown dog jumps over the lazy fox";
    char fmt1[] =   "         1         2         3         4         5";
    char fmt2[] =   "12345678901234567890123456789012345678901234567890";

    void main( void )
    {
       char *pdest;
       int  result;
       printf( "String to be searched:\n\t%s\n", string );
       printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
       pdest = strstr( string, str );
       result = pdest - string + 1;
       if( pdest != NULL )
          printf( "%s found at position %d\n\n", str, result );
       else
          printf( "%s not found\n", str );
    }


    Output

    String to be searched:
       The quick brown dog jumps over the lazy fox
                1         2         3         4         5
       12345678901234567890123456789012345678901234567890

    lazy found at position 36

    0
     
    LVL 5

    Expert Comment

    by:van_dy
    consider the following function:

    #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
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    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.
    0
     
    LVL 5

    Expert Comment

    by:van_dy
    errata:

    int the definition of getindex we should have ..

    int getindex(line, mystring);  //mistakenly typed string earlier
               char *line;
                char *mystring;
    {
     ......
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

    911 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now