Javatpoint Logo
Javatpoint Logo

strchr() function in C

This section will discuss the strchr() function of string headers in the C programming language. An strchr() function is used to find the first occurrence of a specified character within the original string. In other words, an strchr() function checks whether the original string contains defined characters. If the character is found inside the string, it returns a pointer value; otherwise, it returns a null pointer. While using the strchr() function in the C programming language, we need to import the <string.h> header file in the program.

strchr() function in C

Syntax

In the above syntax, an strchr() function has two parameters: str and ch.

str: A str represents the original string in which the character is to be searched.

ch: A ch is a character type variable representing a character searched in the string str.

Return value: It returns a pointer value containing the first occurrence of the character in the given string.

Program to demonstrate the use of a strchr() function

Let's consider an example to check the occurrence of a character in the given string.

Program1.c

Output

Original string is: Use strchr() function in C.
 The first occurrence of the 's' in 'Use strchr() function in C.' string is: 'e strchr() function in C.'

In the above program, we pass an str parameter to the strchr() function to search for character 's', and when a character is found, it returns a pointer ptr value. A ptr variable contains a value from the specified character of the original string until the ch variable does not get a null character in the string.

Program to search a character using the strchr() function and if-else statement

Let's consider an example to get the occurrence of the first character using the strchr() function and if-else statement in a given string of the C programming language.

Program1.c

Output

Original string: "javatpoint"
 Please enter a character you want to search in the string: p

 'p' is found in "javatpoint"

2nd execution:

Original string: "javatpoint"
 Please enter a character you want to search in the string: b

 'b' is not found in "javatpoint"

In the above program, we pass a string "javatpoint" to search for a specified character. Here we take the 'p' character as input from the user and search in the string. After that, the if statement checks for the occurrence of the character using the strchr() function and prints the specified character if it exists. Otherwise, it indicates that the character is not found in the string.

Program to get the occurrence of each character in a given string

Let's consider an example to print the occurrence of each character in a given string with strchr() function and while loop of C programming language.

Program3.c

Output

Given character 'e' found at position 3
 Occurrence of the character 'e' : 1
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "elcome to the JavaTpoint site"

 Given character 'e' found at position 8	
 Occurrence of the character 'e' : 2
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e to the JavaTpoint site"

 Given character 'e' found at position 15
 Occurrence of the character 'e' : 3
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e JavaTpoint site"

 Given character 'e' found at position 31
 Occurrence of the character 'e' : 4
 The occurrence of the character 'e' in the string " Welcome to the JavaTpoint site" is "e"






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA