#include void outputString(char*, int);
char* partString(char*, int, int);
char* revString(char*, int);
bool isReversible(char*, int);
bool strEqual(char*, char*, int);
int main() {
using namespace std;
char* str;
cout << "Confirm the length of the string : ";
int len = 0;
cin >> len;
if(len <= 0) return 0;
cout << endl << "The string please =O= : ";
str = new char[len + 1];
cin.get();
cin.getline(str, len);
cout << endl << "The String : " << str;
cout << endl << "Now outputting ... " << endl;
outputString(str, strlen(str));
cout << endl << "Done, press any key to quit..." << endl;
system("pause");
}
void outputString(char* str, int len) {
for(int i = 2; i < len - 1; i++) {
for(int j = 0; j < len - i; j++) {
char* str2 = partString(str, j, i);
if(isReversible(str2, i))
std::cout << str2 << std::endl;
delete[] str2;
}
}
}
char* partString(char* str, int index, int len) {
char* str2 = new char[len + 1];
for(int i = index; i < index + len; i++)
str2[i - index] = str[i];
str2[len] = '\0';
return str2;
}
bool isReversible(char* str, int len) { //确认一个字符串是否是回文串。
char *str1, *str2;
if(len % 2 == 0) {
str1 = partString(str, 0, len/2);
str2 = revString(partString(str, len/2, len/2), len/2);
} else {
str1 = partString(str, 0, len/2);
str2 = revString(partString(str, len/2 + 1, len/2), len/2);
}
bool b = strEqual(str1, str2, len/2);
delete[] str1, str2;
return b;
}
char* revString(char* str, int len) { //返回一个前后颠倒的字符串。
char* str2 = new char[len + 1];
for(int i = 0; i < len; i++) {
str2[len - i - 1] = str[i];
}
str2[len] = '\0';
return str2;
}
bool strEqual(char* str1, char* str2, int len) { //确认两个字符串是否相等。
for(int i = 0; i < len; i++)
if(str1[i] != str2[i]) return false;
return true;
}
表示要改写成java难度应该不大 咱写的那么认真还测试编译通过了就采纳了吧~QwQ