Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

strstr

Posted on 2001-06-15
5
Medium Priority
?
761 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 80 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

715 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