strchr Function in C Language with Example Programs

strchr-function-in-c-programming-language

Introduction:

We have looked at the strlen, strcpy, strcat, and strcmp library functions in our earlier articles. In today’s article, We will look at the strchr function in C programming Language with example programs.

Prerequisites:

It is good to know the basics of Strings and how to read and print strings in c language. Please go through the following articles to learn more about the Strings in the C language

The strchr Function in C Language:

The strchr() function in C programming is used to search for a character in a string. This function searches for the first occurrence of the given character in the string and returns the pointer to it to the caller.

strchr() function is available as part of the string.h header file. Make sure to include the string.h header file to your program in order to use the strchr() function.

📢 This Program is part of the C String Practice Programs series

Let’s look at the prototype, parameters, and return value of the strchr function in detail.

Syntax of strchr Function in C:

The strchr() function searches for the character ch in the input string str.

Function Parameters:

The strchr() string library function takes two parameters as input. They are

  1. str – String.
  2. ch – Character to search in str string.

Return Type:

The return type of the strchr() function is char *.

If the character ch is found in the str, Then the strchr() function returns a pointer to the first occurrence of the character ch in the input string str.

If the character ch is not found in the input string str, Then the strchr() function return NULL.

Let’s look at an example program to understand the strchr function.

Program to search for a character in a String using strchr Function in C Language:

Here is the Program to search for a character in a string using strchr function in c programming language.

Program Explanation:

Here is the step-by-step explanation of the above program

  1. Add the string.h header file to use the strchr function.
  2. We created a string named str with the SIZE ( 100) characters. and also declared character to search i.e ch and a character pointer( res) to store the result of the search operation.
  3. Prompt the user to enter the desired string and update the str string. Also take the character to search from the user and store it in ch character.
  4. Now we got the string( str) and the character to be searched( ch).
  5. Call the strchr() function with the str and ch to search for the character ch in the string str and store the return value in the res pointer – res = strchr(str, ch);
  6. If found the ch in the str string, Then the res will contain a valid address, If we don’t find the character ch in strong str, Then return value res contains the NULL.
  7. Display result based on the return value res.

Program Output:

Compile and run the program.

Test 1: When the character ch is found in the string str

In this case, User provided the input string as the programming and the character to search as the o.

As we can see from the output, The strchr() function is able to found the character o in the string programming. So the return value res doesn’t contain the NULL value.

strchr-function-in-c-programming-example-programs

Test 2: When the character is not found in the given string.

In this test case, The input string is Hello and the character to be searched is w. As the w character is not found in the Hello string. The strchr() function returned the NULL value. So the if condition if(res == NULL) become true.

Example 2: Return value of the strchr function – Search for a character in string:

Let’s modify the above program and display the return value of the strchr() function.

We have added extra printfstatement to print the string pointed by the resultant pointer res. ( Return value of the strchr() function).

printf("Result is : %s \n", res);

Let’s compile and run the program and observe the output.

As we can see from the above output, User provided the input string as the CProgramming and character as the r.

The strchr() function found the first occurrence of the character r at the 2nd Index(CProgramming). So the strchr() returned the pointer pointing to the first occurrence of the character r. If we print the string at res then we will get the result as rogramming string.

Related Articles:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

3 Responses

  1. […] strchr library function in C […]

  2. […] Function ( strchr library […]

  3. […] Function ( strchr library […]

Leave a Reply