이 기사에서는 C++ STL에서 strchr() 함수의 작동, 구문 및 예제에 대해 논의할 것입니다.
strchr()이란 무엇입니까?
strchr() 함수는
문자열에 문자가 없으면 함수는 널 포인터를 반환합니다.
구문
char* strchr( char* str, char charac );
매개변수
이 함수는 다음 매개변수를 허용합니다.-
-
문자열 − 문자를 찾아야 하는 문자열입니다.
-
문자 − str 문자열에서 찾고자 하는 문자입니다.
반환 값
이 함수는 문자열에서 문자가 처음 나타난 위치에 대한 포인터를 반환합니다. 문자를 찾지 못하면 null 포인터를 반환합니다.
입력 -
char str[] = "Tutorials Point"; char ch = ‘u’;
출력 - u는 문자열에 있습니다.
예
#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