在本文中,我们将讨论strchr()C ++ STL中函数的工作,语法和示例。
strchr()啊strchr()函数是C ++ STL中的内置函数,在<cstring>头文件中定义。strchr()函数用于查找字符首次出现在字符串中的时间。此函数将指针返回到字符首次出现在字符串中的位置。
如果字符串中不存在该字符,则该函数返回空指针。
char* strchr( char* str, char charac );
该函数接受以下参数-
str-这是我们必须在其中查找字符的字符串。
charac-这是我们要在字符串str中搜索的字符。
此函数返回一个指针,该指针指向字符首次出现在字符串中的位置。如果找不到该字符,则返回空指针。
输入-
char str[] = "nhooo.com"; char ch = ‘u’;
输出-字符串中存在u。
#include <iostream>
#include <cstring>
using namespace std;
int main(){
char str[] = "nhooo.com";
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[] = "nhooo.com";
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 : nhooo.com is a learning portal