Solved

strstr

Posted on 2001-06-15
5
749 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Answer is very simple and clear, great thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

17 Experts available now in Live!

Get 1:1 Help Now