Understanding the strchr function in C programming language can be useful for multiple good reasons. Hence
we are going to examine the strchr function in this article. In order to deliver clarity for the reader, the
information in this tutorial is divided into 4 relevant parts:
Return: pointer to the first occurence of the int c character inside of const
char *str array. If none is found, return is null.
Parameter list: (const char *str) - the character array to be checked if it
contains the int c value. (int c) - the value to be searched for in a const char *str array.
Header file:<string.h>.
1. So, what is the strchr function in C?
The strchr function is defined in the <string.h> library.
This is indicative that the strchr function deals with strings.
The strchr function takes in two parameters, namely, a char
array and a char. Within this function, then, it is
checked if the char, that is received as a second parameter,
can be found within the char array.
If the character is found within the character array, then a
pointer to the first occurence of this character inside the char
array is returned.
If the character is not found within the character array, then
NULL is returned.
To use the strchr function in our C programs, we have to include the <string.h> library (e.g.
#include <string.h>).
Example 1: usage of the strchr function in C.
#include<string.h>
#include<stdio.h>
intmain(void)
{
printf("1.%s\n",strchr("This is a test", 'a'));
printf("2.%s",strchr("Can a digit be found?", '2'));
return(0);
}
Possible output:
1. a test
2. (null)
2. How to rewrite the strchr function in C?
Consider the code snippet below for a second:
Example 2: Rewritten strchr function in C.
char *our_strchr(const char *str,intc)
{
char a;
int i;
if(s==NULL)
return(NULL);
a=c;
i=0;
while(s[i] != '\0' && s[i] != a)
i++;
if(s[i]==a)
return((char*)(s+i));
return(NULL);
}
In the our_strchr function you can see a while loop (on line
9). This loop continues until one of two conditions are met. (1) The character c is found or (2) a null terminating character ('\0') is found (indicating the end of the character array).
After this while loop, the program checks if the condition (on line
11) is true. This condition, in turn,
checks if the c character caused the while loop to break. If it
is the case, this condition will return true and pointer pointing to the first occurrence of this
character in the str array will be returned.
If the same condition on (on line
11) is false, it indicates that the character was not found. In which case our_strchr returns NULL.
Note: (on line 5) we are checking if the str array is
not equal to NULL. Which protects this function
from segmentation faults, in case NULL is passed as an argument.
3. Testing the rewritten strchr function
In order to test the our_strchr function, feel free to run the
code below:
Example 3: Test of the rewritten strchr function in C.
#include<string.h>
#include<stdio.h>
char *our_strchr(const char *str,intc)
{
char a;
int i;
if(s==NULL)
return(NULL);
a=c;
i=0;
while(s[i] != '\0' && s[i] != a)
i++;
if(s[i]==a)
return((char*)(s+i));
return(NULL);
}
intmain(void)
{
printf("1.%s\n",our_strchr("This is a test", 'a'));
printf("2.%s",our_strchr("Can a digit be found?", '2'));
return(0);
}
Possible output:
1. a test
2. (null)
As you can see, the rewritten strchr function returns the same results as the C library function discussed above. In addition, it is
protected against segmentation faults.