#include <stdio.h>
#include <stdlib.h>
// 文字列の長さを返す関数
int myStrlen(char s[]) {
int i;
for (i = 0; s[i] != '\0'; i++);
return i;
}
// 回文を作成する関数
char *setPalindrome(char s[]) {
int len = myStrlen(s); // 入力文字列の長さを取得
int totalLen = 2 * len; // 回文用のメモリサイズを計算
char *tmp
= (char *)malloc((totalLen
+ 1) * sizeof(char)); // メモリ確保
if (tmp == NULL) {
printf("Memory allocation failed\n"); exit(1); // メモリ確保に失敗した場合にプログラム終了 }
// 入力文字列をコピー
for (int i = 0; i < len; i++) {
tmp[i] = s[i];
}
// 入力文字列を逆順に追加
for (int i = 0; i < len; i++) {
tmp[len + i] = s[len - 1 - i];
}
tmp[totalLen] = '\0'; // 終端文字を追加
return tmp; // 回文を返す
}
int main() {
char nyuryoku[1024]; // 入力
char *kaibun; // 回文を受け取る
kaibun = setPalindrome(nyuryoku);
printf("%s\n -> %s\n", nyuryoku
, kaibun
); free(kaibun
); // 動的確保したメモリを解放 return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIOaWh+Wtl+WIl+OBrumVt+OBleOCkui/lOOBmemWouaVsAppbnQgbXlTdHJsZW4oY2hhciBzW10pIHsKICAgIGludCBpOwogICAgZm9yIChpID0gMDsgc1tpXSAhPSAnXDAnOyBpKyspOwogICAgcmV0dXJuIGk7Cn0KCi8vIOWbnuaWh+OCkuS9nOaIkOOBmeOCi+mWouaVsApjaGFyICpzZXRQYWxpbmRyb21lKGNoYXIgc1tdKSB7CiAgICBpbnQgbGVuID0gbXlTdHJsZW4ocyk7IC8vIOWFpeWKm+aWh+Wtl+WIl+OBrumVt+OBleOCkuWPluW+lwogICAgaW50IHRvdGFsTGVuID0gMiAqIGxlbjsgLy8g5Zue5paH55So44Gu44Oh44Oi44Oq44K144Kk44K644KS6KiI566XCiAgICBjaGFyICp0bXAgPSAoY2hhciAqKW1hbGxvYygodG90YWxMZW4gKyAxKSAqIHNpemVvZihjaGFyKSk7IC8vIOODoeODouODqueiuuS/nQoKICAgIGlmICh0bXAgPT0gTlVMTCkgewogICAgICAgIHByaW50ZigiTWVtb3J5IGFsbG9jYXRpb24gZmFpbGVkXG4iKTsKICAgICAgICBleGl0KDEpOyAvLyDjg6Hjg6Ljg6rnorrkv53jgavlpLHmlZfjgZfjgZ/loLTlkIjjgavjg5fjg63jgrDjg6njg6DntYLkuoYKICAgIH0KCiAgICAvLyDlhaXlipvmloflrZfliJfjgpLjgrPjg5Tjg7wKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKICAgICAgICB0bXBbaV0gPSBzW2ldOwogICAgfQoKICAgIC8vIOWFpeWKm+aWh+Wtl+WIl+OCkumAhumghuOBq+i/veWKoAogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgIHRtcFtsZW4gKyBpXSA9IHNbbGVuIC0gMSAtIGldOwogICAgfQoKICAgIHRtcFt0b3RhbExlbl0gPSAnXDAnOyAvLyDntYLnq6/mloflrZfjgpLov73liqAKICAgIHJldHVybiB0bXA7IC8vIOWbnuaWh+OCkui/lOOBmQp9CgppbnQgbWFpbigpIHsKICAgIGNoYXIgbnl1cnlva3VbMTAyNF07IC8vIOWFpeWKmwogICAgY2hhciAqa2FpYnVuOyAgIC8vIOWbnuaWh+OCkuWPl+OBkeWPluOCiwogICAgc2NhbmYoIiVzIiwgbnl1cnlva3UpOwogICAga2FpYnVuID0gc2V0UGFsaW5kcm9tZShueXVyeW9rdSk7CiAgICBwcmludGYoIiVzXG4gIC0+ICVzXG4iLCBueXVyeW9rdSwga2FpYnVuKTsKICAgIGZyZWUoa2FpYnVuKTsgLy8g5YuV55qE56K65L+d44GX44Gf44Oh44Oi44Oq44KS6Kej5pS+CiAgICByZXR1cm4gMDsKfQo=