C ++およびそのアプリケーションのstrchr()関数
この記事では、C ++ STLでのstrchr()関数の動作、構文、および例について説明します。
strchr()とは何ですか?
strchr()関数は、C ++ STLに組み込まれている関数であり、
文字列に文字が存在しない場合、関数はnullポインタを返します。
構文
char* strchr( char* str, char charac );
パラメータ
この関数は、次のパラメーターを受け入れます-
-
str −文字を探す必要がある文字列です。
-
文字 −文字列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
-
C ++のstrchr()関数
C ++では、strchr()は事前定義された関数です。文字列の処理に使用され、指定された文字列内の特定の文字の最初の出現を返します。 strchr()の構文は次のとおりです。 char *strchr( const char *str, int c) 上記の構文で、strは文字cを含む文字列です。 strchr()関数は、str内で最初に出現するcを検出します。 strchr()関数を示すプログラムは次のとおりです。 例 #include <iostream> #include <cstring> using namespace std; int main()
-
C++でのフレンドクラスと関数
クラスのフレンド関数はそのクラスのスコープ外で定義されていますが、クラスのすべてのプライベートメンバーと保護されたメンバーにアクセスする権利があります。フレンド関数のプロトタイプはクラス定義に表示されますが、フレンドはメンバー関数ではありません。 フレンドは、関数、関数テンプレート、メンバー関数、またはクラスまたはクラステンプレートにすることができます。この場合、クラス全体とそのすべてのメンバーがフレンドになります。 関数をクラスのフレンドとして宣言するには、次のように、クラス定義の関数プロトタイプの前にキーワードfriendを付けます- class Box { double width;