Parsi Coders

نسخه‌ی کامل: نحوه عملکرد توابع 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