Finding the first occurrence of a substring in the given string using strstr() C function
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows 2003 Server Standard Edition
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info:
To do: Finding the first occurrence of a substring in the given string using strstr() C function
To show: How to use the strstr() C function to return a pointer to the first occurrence of a search string in a string
// Using strstr()/wcsstr()/_mbsstr() function
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string1 = "abcdefgabcdefgabcdefg";
char *string2 = "defg";
printf(" Using strstr()\n");
printf(" ---------------\n");
printf("string1 = %s\n", string1);
printf("string2 = %s\n", string2);
printf("\nThe remainder of string1 beginning with the");
printf("\nfirst occurrence of string2 is: %s\n", strstr(string1, string2));
return 0;
}
Output example:
Using strstr()
---------------
string1 = abcdefgabcdefgabcdefg
string2 = defg
The remainder of string1 beginning with the
first occurrence of string2 is: defgabcdefgabcdefg
Press any key to continue . . .