Parsi Coders
نحوه عملکرد توابع strchr و strrchr - نسخه قابل چاپ

+- Parsi Coders (http://parsicoders.com)
+-- انجمن: Software Development Programming (http://parsicoders.com/forumdisplay.php?fid=37)
+--- انجمن: C and C++ (http://parsicoders.com/forumdisplay.php?fid=54)
+--- موضوع: نحوه عملکرد توابع strchr و strrchr (/showthread.php?tid=2305)



نحوه عملکرد توابع strchr و strrchr - Ghoghnus - 05-24-2012

کد:
#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