Finding a character in a string using strchr() 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 last occurrences of a character in the given string using strchr() C function

To show: How to use the strchr()/wcschr()/_mbschr() C function to find a character in the given string

 

// Using strchr()

#include <stdio.h>

#include <string.h>

 

int main(void)

{

char *string1 = "A zoo has many animals including birds";

int c = 'm';

 

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

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

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

printf("\nThe remainder of string1 beginning with the\n");

printf("last occurrence of character \'%c\'", c);

printf("\nis: %s\n", strchr(string1, c));

 

return 0;

}

 

Output example:

 

Using strchr()

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

string1 = A zoo has many animals including birds

The remainder of string1 beginning with the

last occurrence of character 'm'

is: many animals including birds

Press any key to continue . . .

 

 

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