The strchr function finds the first occurrence of c (converted to a char) in the string pointed to by s. The terminating null character is also considered to be part of the string and it can be found if searching for ‘\0’. The strchr function defines in string.h header file and takes two arguments.
Syntax of strchr function:
//Syntax of strchr char *strchr(const char *s, int c);
Parameters:
s: This is a pointer to the null-terminated byte string
c: This is a character to search for.
Return:
The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.
Sometimes peoples require to create their own strchr function for their project. So here in the below section, I shall describe the way to create the strchr() function or you can say that we will see the implementation of strchr in C.
Let’s see an example code to understand the functionality of the strchr in C. In this C code, we will try to find a character in a given character array.
#include <stdio.h> #include <string.h> int main() { //string const char *s = "Aticleworld"; //char want to search char want_search = 'c'; char *ptr = strchr(s,want_search); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
When you run the program, the output will be:
‘c’ found at position 4.
search character found: cleworld
Explanation:
In the above C program, we are searching character ‘c’ in the given string “Aticleworld”. The strchr function starts searching from the first character ‘A’. to the null character. When it will find the ‘c’ it returns the address of character ‘c’.
Highlighted points with some application of strchr() in C:
1.) It takes two parameters and you must include string.h header file in your C program.
2.) The strchr library function finds the first occurrence of c (converted to a char) in the string pointed to by s.
3.) If the character does not occur in the string, then the strchr function returns a null pointer.
#include <stdio.h> #include <string.h> int main() { //string const char *s = "Aticleworld"; //char want to search char want_search = 'b'; char *ptr = strchr(s,want_search); if (ptr != NULL) { printf ("'b' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output:
search character not found
4.) The strchr function is used to remove trailing newline character from fgets input. Let’s see an example code where I am removing the newline (‘\n’) from fgtes input.
#include <stdio.h> #include <string.h> #define BUFFER_SIZE 24 int main() { char buf[BUFFER_SIZE]; printf("Enter the data = "); if (fgets(buf, sizeof(buf), stdin) == NULL) { printf("Fail to read the input stream"); } else { char *ptr = strchr(buf, '\n'); if (ptr) { *ptr = '\0'; } } printf("Entered Data = %s\n",buf); return 0; }
5.) strchr() function can also be used to check the presence of a character in a given string.
How to write your own strchr() function in C?
Your compiler/standard library will likely have a very efficient and tailored implementation of the strchr() function. So if not require avoid to create own version of the strchr function.
Note: Below function only to understand the working of strchr. There is a lot of scenarios that are not handled in this function. You can handle the scenario as your requirement and if possible then use the standard strchr function (library function).
char *my_strchr(const char *s, int c) { char *isCharFind = NULL; //handling null pointer if(s != NULL) { do { if( *s == (char)c ) { isCharFind=s; break; } } while(*s++); } return isCharFind; }
How it works:
The my_strchr() function takes two arguments one void pointers (void *) for pointing to string and one integer for the character which you want to search in the string.
In this function, we iterating a loop to find the character in the given string. We are also verifying that s must not be a null pointer.
In the do-while loop, if we find the character, then we are storing the address of the character in a pointer isCharFind and breaking the while loop using the break statements.
do { if( *s == (char)c ) { isCharFind=s; break; } } while(*s++);
Let’s write a small application to test our own created strchr function,
#include <stdio.h> char *my_strchr(const char *s, int c) { char *isCharFind = NULL; //handling null pointer if(s != NULL) { do { if( *s == (char)c ) { isCharFind=s; break; } } while(*s++); } return isCharFind; } int main() { char s[] = "Aticleworld"; //called own created memchr function char *ptr = my_strchr(s,'c'); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Differences between memchr and strchr (memchr vs strchr):
There is some basic difference between memchr and strchr function. I am describing these differences point by point.
1.) memchr takes three parameters while strchr takes two parameters.
2.) We pass the max length of bytes which want to scan in memchr but in strchr, we don’t need to pass the number of bytes.
3.) The memchr does not expect null character for the first parameter while strchr expects null character and it works only on the string.
4.) The memchr does not stop when it hits a null character but strchr stop when it hits a null character. Let’s see C code examples where I am using strchr and memchr.
Example with strchr:
#include <stdio.h> #include <string.h> int main() { // embedded null byte const char s[] = "ABCDE\0abc"; //Use of strchr to find 'c' char *ptr = strchr(s,'c'); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output when running the above code:
search character not found
Example with memchr:
#include <stdio.h> #include <string.h> int main() { // embedded null byte const char s[] = "ABCDE\0abc"; //Use of memchr to find 'c' char *ptr = memchr(s,'c',sizeof(s)); if (ptr != NULL) { printf ("'c' found at position %d.\n", ptr-s+1); printf ("search character found: %s\n", ptr); } else { printf ("search character not found\n"); } return 0; }
Output when running the above code:
‘c’ found at position 9.
search character found: c
Recommended Articles for you:
- Use of memchr in C programming.
- strrchr in C programming.
- How to use strncmp function in C.
- Implement and use of memset of in C
- How to make memcpy function in C
- Implement own memmove in C.
- memmove vs memcpy.
- Implement vector in C.
- How to Use strncpy() and implement own strncpy().
- Implement your own strcat in C.