برنامه ای که یادآوری: نحوه عملکرد توابع strchr و strrchr را نشان میدهد:
#include <iostream>
#include <string>
using namespace std;
void main(void)
{
char ch='r';
char string[] =
"The quick brown dog jumps over the lazy fox";
char fmt1[] =
" 1 2 3 4 5";
char fmt2[] =
"12345678901234567890123456789012345678901234567890";
char* pdest;
int result;
cout<<"String to be searched: \n\t\t"<<string<<"\n";
cout<<"\t\t"<<fmt1<<"\n\t\t"<<fmt2<<"\n\n";
cout<<"Search char:\t"<<ch<<"\n";
/* Search forward. */
pdest=strchr(string, ch);
result=pdest - string + 1;
if( pdest != NULL )
cout<<"Result:\tfirst "<<ch
<<" found at position "<<result<<"\n\n";
else
cout<<"Result:\t"<<ch<<" not found\n";
/* Search backward. */
pdest=strrchr(string, ch);
result=pdest - string + 1;
if( pdest != NULL )
cout<<"Result:\tfirst "<<ch
<<" found at position "<<result<<"\n\n";
else
cout<<"Result:\t"<<ch<<" not found\n";
}
//end
#include <string>
using namespace std;
void main(void)
{
char ch='r';
char string[] =
"The quick brown dog jumps over the lazy fox";
char fmt1[] =
" 1 2 3 4 5";
char fmt2[] =
"12345678901234567890123456789012345678901234567890";
char* pdest;
int result;
cout<<"String to be searched: \n\t\t"<<string<<"\n";
cout<<"\t\t"<<fmt1<<"\n\t\t"<<fmt2<<"\n\n";
cout<<"Search char:\t"<<ch<<"\n";
/* Search forward. */
pdest=strchr(string, ch);
result=pdest - string + 1;
if( pdest != NULL )
cout<<"Result:\tfirst "<<ch
<<" found at position "<<result<<"\n\n";
else
cout<<"Result:\t"<<ch<<" not found\n";
/* Search backward. */
pdest=strrchr(string, ch);
result=pdest - string + 1;
if( pdest != NULL )
cout<<"Result:\tfirst "<<ch
<<" found at position "<<result<<"\n\n";
else
cout<<"Result:\t"<<ch<<" not found\n";
}
//end
- برچسب ها: strchr، strrchr، stringchr، string characters، رشته، تابع، جستجو، جستجو کاراکتر، جستجو رشته، سرچ،
تابع strcspn: این تابع کاراکتر های یک رشته را در یک رشته جستجو کرده و مکان اولین موردی که یکی از کاراکتر های زیر رشته در رشته ی اصلی وجود داشته باشد را به صورت یک عدد int برمیگرداند. شکل کلی بکارگیری این تابع بصورت زیر است:
;(رشته 2 ,رشته 1)int position=strcspn
- برچسب ها: رشته، جستجو، تابع، strcspn، stringcspn، تابع جستجو، جستجو رشته، جستجو در رشته،
- کل صفحات:0
تبلیغات
