[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

finding specific char substring

Posted on 2004-10-22
4
Medium Priority
?
243 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
Comment
Question by:siggyjav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 12385046
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
ID: 12385068
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
ID: 12385155
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
ID: 12385189
errata:

int the definition of getindex we should have ..

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

649 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