Strchr Function In C

Syntax

Example

char *strchr (const char *str, int c);

In the provided syntax, the strchr function accepts two arguments: str and ch.

str: The variable str denotes the initial string where the character will be searched.

A ch variable is a data type that represents a character and is used to denote a specific character being sought within the string str.

It provides a pointer value containing the initial instance of the character within the specified string.

Program to demonstrate the use of a strchr function

Let's explore an illustration to verify the presence of a character within a specified string.

Program1.c

Example

#include <stdio.h>
#include <string.h>
int main ()
{
	const char str[] = "Use strchr() function in C.";
	const char ch = 's'; // it is searched in str[] array
	char *ptr; // declare character pointer ptr
	
	printf (" Original string is: %s \n", str);
	// use strchr() function and pass str in which ch is to be searched
	ptr = strchr( str, ch);
	printf (" The first occurrence of the '%c' in '%s' string  is: '%s' ", ch, str, ptr);
	return 0;
}

Output

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 provided code snippet, we provide an str argument to the strchr function in order to locate the character ' s '. Upon identifying the character, the function returns a pointer value assigned to the ptr variable. This ptr variable stores a value starting from the located character within the initial string until the ch variable encounters a null character within the string.

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

Let's explore a scenario where we determine the position of the initial character within a specified string in the C programming language by utilizing the strchr function in conjunction with an if-else statement.

Program1.c

Example

#include <stdio.h>
#include <string.h> // use string.h header file 

int main ()
{
	
	const char *s = "logic practice"; // initialization of the constant char pointer
	char ch; // declare a ch variable
	
	printf (" Original string: \"%s\" \n ", s); // print the string
	
	// take a character from the user 
	printf ("Please enter a character you want to search in the string: ");
	scanf (" %c", &ch);
	
	// it checks whether the specified character exists in the string
	if ( strchr (s, ch) != NULL )
	{
		printf (" \n '%c' is found in \"%s\" ", ch, s);
		}
	// if the character is not found, the below statement is executed		
	else
		printf (" \n '%c' is not found in \"%s\" ", ch, s);
	
	return 0;	
}

Output

Output

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

 'p' is found in "logic practice"

2 nd execution:

Example

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

 'b' is not found in "logic practice"

In the preceding code snippet, a string "logic practice" is provided for locating a particular character. The user provides input for the character 'p' to be searched within the string. Subsequently, an if statement verifies the presence of the character by employing the strchr function. If the character is found, it is displayed; otherwise, a message notifies that the character is not present in the string.

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

Let's explore a scenario where we demonstrate how to display the frequency of each character in a provided string using the strchr function and a while loop in the C programming language.

Program3.c

Example

#include <stdio.h>
#include <string.h>
int main ()
{
	// initialize a string
	char str[] = " Welcome to the Logic Practice site";
	char *ptr; // declare a pointer variable
	int i = 1; // declare and initialize i
	
	// use strchr() function to check the occurrence of the character 
	ptr = strchr (str, 'e' );
	
	// use while loop to checks ptr until it becomes null
	while (ptr != NULL)
	{
		printf (" Given character 'e' found at position %d \n", (ptr - str + 1));
		printf (" Occurrence of the character 'e' : %d \n", i);
		printf (" The occurrence of the character 'e' in the string \"%s\" is \"%s\" \n \n", str, ptr);
		
		// use strchr() function to update the position of the string
		ptr = strchr (ptr + 1, 'e');
		i++;
	}
	return 0;
}

Output

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 Logic Practice site" is "elcome to the Logic Practice 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 Logic Practice site" is "e to the Logic Practice 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 Logic Practice site" is "e Logic Practice 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 Logic Practice site" is "e"

Input Required

This code uses input(). Please provide values below: