Finding a substring in the given string using memchr() 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 a substring in the given string using C function memchr()

To show: How to use the memchr() C function to find a substring in the given string

 

// Using memchr()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *s = "This is a test string";

char p = 'e';

 

printf("Using memchr()\n");

printf("--------------\n");

printf("char p = \'e\'\n");

printf("s = %s\n", s);

printf("\nThe remainder of string s, after character \'%c\'", p);

printf("\nis found, using memchr(s, p, 15)");

printf("\nis \"%s\"\n", memchr(s, p, 15));

 

return 0;

}

 

Output example:

 

Using memchr()

--------------

char p = 'e'

s = This is a test string

The remainder of string s, after character 'e'

is found, using memchr(s, p, 15)

is "est string"

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index