strchr() क्या है?
strchr() C++ STL में एक अंतर्निहित फंक्शन है, जो <cstring> हेडर फाइल में परिभाषित है। strchr() फंक्शन का उपयोग स्ट्रिंग में किसी कैरेक्टर की पहली घटना खोजने के लिए किया जाता है। यह फंक्शन उस स्थान का पॉइंटर लौटाता है जहां कैरेक्टर पहली बार स्ट्रिंग में प्रकट हुआ।
यदि कैरेक्टर स्ट्रिंग में मौजूद नहीं है तो फंक्शन null पॉइंटर लौटाता है।
सिंटैक्स
char* strchr(char* str, char charac);
पैरामीटर्स
str - यह वह स्ट्रिंग है जिसमें हमें कैरेक्टर खोजना है।
charac - यह वह कैरेक्टर है जिसे हम स्ट्रिंग str में खोजना चाहते हैं।
रिटर्न वैल्यू
यह फंक्शन उस स्थान का पॉइंटर लौटाता है जहां कैरेक्टर पहली बार स्ट्रिंग में प्रकट हुआ। यदि कैरेक्टर नहीं मिलता तो यह null पॉइंटर लौटाता है।
उदाहरण
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Tutorials Point";
char ch_1 = 'b', ch_2 = 'T';
if (strchr(str, ch_1) != NULL)
cout << ch_1 << " " << "is present in string" << endl;
else
cout << ch_1 << " " << "is not present in string" << endl;
if (strchr(str, ch_2) != NULL)
cout << ch_2 << " " << "is present in string" << endl;
else
cout << ch_2 << " " << "is not present in string" << endl;
return 0;
}
आउटपुट
b is not present in string T is present in string
उदाहरण
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str[] = "Tutorials Point";
char str_2[] = " is a learning portal";
char ch_1 = 'b', ch_2 = 'T';
if (strchr(str, ch_1) != NULL) {
cout << ch_1 << " " << "is present in string" << endl;
} else {
cout << ch_1 << " " << "is not present in string" << endl;
}
if (strchr(str, ch_2) != NULL) {
cout << ch_2 << " " << "is present in string" << endl;
strcat(str, str_2);
cout << "String after concatenation is: " << str;
} else {
cout << ch_2 << " " << "is not present in string" << endl;
}
return 0;
}
आउटपुट
b is not present in string T is present in string String after concatenation is: Tutorials Point is a learning portal