strstr function

 

strstr will find a sub-string within a string. See also strchr which will look for a character in a string.

Library:   string.h

Prototype: char * strstr(const char *s1, const char *s2);

Syntax: char string1[]="red dwarf";
char string2[]="dwarf";
void *pointer;
pointer = strstr(string1, string2);

strstr returns a pointer to the beginning of the sub-string or NULL if not found.

 

 

/*****************************************************************
*
* Purpose: Program to demonstrate the 'strstr' function.
* Author: M J Leslie
* Date: 18-Jun-94
*
*****************************************************************/

#include <stdio.h>
#include <string.h>

main()
{
char string[]="string to search";
char test[]="sear";

/* strstr returns a pointer into 'string'
* if 'test' is found' if not found, NULL
* is returned. */

if (strstr(string, test)) puts("String found");

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章