給定兩個字串s1,s2,請編寫程式碼檢查s2是否為s1旋轉而成
阿新 • • 發佈:2018-12-24
#include <iostream>
#include <string>
#include <cstdio> // getchar()
using namespace std;
/************************************************************************/
// 函式名稱:isRotation_2
// 函式目的:判斷字串是否是轉換字串
// 函式引數:s1 s2
// 函式返回:true:是轉置字串
// 使用條件:
/************************************************************************/
bool isRotation_2(const string& s1, const string& s2)
{
size_t len = s1.length();
if (len == s2.length() && len > 0){
string s1s1 = s1 + s1; // the key point
size_t found = s1s1.find(s2);
if ( found != std::string::npos ) return true;
}
return false ;
}
int main()
{
char s1[] = "waterbottle";
char s2[] = "erbottlewat";
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "呼叫轉換字元函式isRotation_2()判斷之後:" << endl;
cout << (isRotation_2(s1, s2) ? "true" : "false" ) << endl;
getchar();
return 0;
}
#include <string>
#include <cstdio> // getchar()
using namespace std;
/************************************************************************/
// 函式名稱:isRotation_2
// 函式目的:判斷字串是否是轉換字串
// 函式引數:s1 s2
// 函式返回:true:是轉置字串
// 使用條件:
/************************************************************************/
bool isRotation_2(const string& s1, const string& s2)
{
size_t len = s1.length();
if (len == s2.length() && len > 0){
string s1s1 = s1 + s1; // the key point
size_t found = s1s1.find(s2);
if ( found != std::string::npos ) return true;
}
return false
}
int main()
{
char s1[] = "waterbottle";
char s2[] = "erbottlewat";
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "呼叫轉換字元函式isRotation_2()判斷之後:" << endl;
cout << (isRotation_2(s1, s2) ? "true" : "false"
getchar();
return 0;
}