#include<iostream> #include<cstring> #include<string> using namespace std; bool isSuffix(string s1, string s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; }
bool isPrefix(string s1, string s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<n1; i++) if (s1[i] != s2[i]) return false; return true; }
bool Substring(string s1, string s2) { int n1 = s1.length(), n2 = s2.length(); if (n1 > n2) return false; for (int i=0; i<s2[i]!='\0'; i++) if (s1[i]!=s2[i]) return false; return true; } int main() { //getline(cin,s1); //getline(cin,s2);
string s1 = "saat", s2 = "aaabsaat"; bool result1 = isSuffix(s1, s2); bool result2 = isPrefix(s1, s2); bool result3 = Substring(s1, s2); if(result1 || result2 || result3) { if (result1) cout << "Suffix"<<endl; if (result2) cout << "Prefix"<<endl; if (result3) cout << "Sub-String"<<endl; } else { cout<<"Not suffix, prefix or sub-string"<<endl; } return 0; }
<-------------------------->===<-------------------------->
sample input:
saat
aaabsaat
sample output:
Suffix
Tags:
C++