In C++, strchr() is a predefined function used for finding occurrence of a character in a string. It is present in cstring header file.
// Returns pointer to the first occurrence // of c in str char *strchr(const char *str, int c)
Note that c is passed as its int promotion, but it is internally treated as char.
Given a string in c++, we need to find the first occurrence of a character, lets say ‘a’.
Input : str = 'This is a string' Output : 9 Input : str = 'My name is Ayush' Output : 4
1. Pass the given string in the strchr() function and mention the character you need to point.
2. The function return a value, print the value.
strch() function can also be used to check the presence of a character in a strinG. The input consists of a character we want to check, if it exists in the string.
For example – Lets check if the characters A and z are present in the string – “My name is Ayush”
Input : str = 'My name is Ayush', ch1 = 'A', ch2 = 'z' Output : A is present in the string z is not present in the string
1. Pass the given string in the strchr() function with the character as the second parameter and check if the value returned is not null
2. If the function returns a NULL value, this means that the string does not contains the character, so, print the required statement.
3. Else if the function does not returns a NULL value, this means that the string contains the character, so, print the required statement.
A is present in string z is not present in string
Strchr function can be used to find absolute directory path for Linux: (Contributed by Ekta_nehwal)
Input : /home/test/sample Output : /home/test
- Find the position of last “/” in the directory path by using strrchr.
- Replace the occurrence with the NULL character.
This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C++ string class and its applications
- C++ String Class and its Applications | Set 2
- stringstream in C++ and its applications
- unordered_multiset and its uses
- C++ bitset and its application
- unordered_multimap and its application
- isspace() in C/C++ and its application to count whitespace characters
- iscntrl() in C++ and its application to find control characters
- Bind function and placeholders in C++
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- getline() function and character array
- isupper() and islower() and their application in C++
- fesetround() and fegetround() in C++ and their application
- Comparator function of qsort() in C
- strftime() function in C/C++
- std::gcd | C++ inbuilt function for finding GCD
- Power Function in C/C++
- wcscpy() function in C++ with Examples
- wcscmp() function in C++ with Examples
- strtoul() function in C/C++