Solved

strstr

Posted on 2001-06-15
5
757 Views
Last Modified: 2008-02-26
could you guys explain what does strstr function do?
thanks
0
Comment
Question by:zunera
[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
5 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6196524
char *strstr(const char *string, const char *strCharSet)

The strstr function returns a pointer to the first occurrence of strCharSet in string.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6196525
Example

/* 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 32

Accepted Solution

by:
jhance earned 20 total points
ID: 6196528
strstr locates a SUBSTRING within another string.

Let's say you have:

char *str1 = "This is only a test";
char *p;

and you do:

p = strstr(str1, "only");

p will point to the "o" in only.  If, however, you did:

p = strstr(str1, "ONLY");

p will be NULL since the string "ONLY" does not appear in the string str1.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6196534
I just copied and pasted this from MSN help document.
See following link for more info.
http://msdn.microsoft.com/library/devprods/vs6/visualc/vccore/_crt_strstr.2c_.wcsstr.2c_._mbsstr.htm
0
 

Author Comment

by:zunera
ID: 6196612
Answer is very simple and clear, great thanks
0

Featured Post

Technology Partners: 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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 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