In C++, strchr() is a predefined function used for finding the occurrence of a character in a string. It is present in cstring header file.
Syntax:
// 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.
Application
Given a string in c++, we need to find the first occurrence of a character, let’s say ‘a’.
Examples:
Input : str[] = ‘This is a string’
Output : 9
Input : str[] = ‘My name is Ayush’
Output : 5
Algorithm:
1. Pass the given string in the strchr() function and mention the character you need to point.
2. The function returns a value, and print the value.
Below is the implementation of the above algorithm:
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "My name is Ayush" ;
char * ch = strchr (str, 'a' );
cout << ch - str + 1;
return 0;
}
|
strchr() 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.
Example: Let’s 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
Algorithm
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 contain 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.
Below is the implementation of the above algorithm:
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char str[] = "My name is Ayush" ;
char ch = 'A' , ch2 = 'z' ;
if ( strchr (str, ch) != NULL)
cout << ch << " "
<< "is present in string" << endl;
else
cout << ch << " "
<< "is not present in string" << endl;
if ( strchr (str, ch2) != NULL)
cout << ch2 << " "
<< "is present in string" << endl;
else
cout << ch2 << " "
<< "is not present in string" << endl;
return 0;
}
|
Output
A is present in string
z is not present in string
strrchr() in C++
The strrchr() function is a similar function to strchr() but this function returns the pointer to the last occurrence of the given character in the string.
Applications:
Some of the application of strrchr() is:
strchr() function can be used to find absolute directory path for Linux: (Contributed by Ekta_nehwal)
Examples:
Input : /home/test/sample
Output : /home/test
Algorithm:
- Find the position of last “/” in the directory path by using strrchr.
- Replace the occurrence with the NULL character.
Below is the implementation of the above algorithm, basically we are finding the position of / and then changing it to \0(null).
C
#include <string.h>
#include<stdio.h>
int main()
{
char string[]={ "/home/test/sample" };
int len;
char * pos;
len = strlen (string);
pos = strrchr (string, '/' ) ;
printf ( "%s\n" ,string);
*pos= '\0' ;
printf ( "%s\n" ,string);
return 0;
}
|
Output
/home/test/sample
/home/test
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 Nov, 2023
Like Article
Save Article