C ++中的strchr()函数及其应用
在本文中,我们将讨论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
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短