首页 > 代码库 > 字串的先后顺序

字串的先后顺序

技术分享
 1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <string> 5 #include <stdlib.h> 6 using namespace std; 7  8 int main(int argc, char * argv[]) 9 {10     char mn[10000] = {0};11     char first[100] = {0};12     char second[100] = {0};13     while (scanf("%s", mn) != EOF) {14         getchar();15         string str(mn);16         int i;17         scanf("%s", first);18         getchar();19         string fstr(first);20         scanf("%s", second);21         getchar();22         string sstr(second);23 24         int idxf = str.find(fstr, 0);25         int idxfr = str.rfind(fstr);26         int idxs = str.find(sstr, 0);27         //int idxsr = str.find(sstr, 0);28         if((idxf == string::npos) || (idxs == string::npos)){29             cout << "invalid" << endl;30         }31 32         if((idxfr > idxf) && (idxf < idxs) && (idxfr > idxs)){33             cout << "both" << endl;34         }35 36         if (idxfr == idxf)37             if(idxf < idxs){38                 cout << "forward" << endl;39             }else if(idxf > idxs){40                 cout << "backward" << endl;41             }42 43 44     }45     return 0;46 }
View Code

 

eg:
input:
atob
a
b
output:
forwad

aacacaa
cac
aa
output:
both

字串的先后顺序