Solved

strstr

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 322
Grammars for C C++ and java 1 131
Unresolved External Symbols 3 109
Finding Divisors 5 20
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

820 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