#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 i;
char *tmp;
// 動的にメモリを確保
tmp
= (char *)malloc((2 * len
+ 1) * sizeof(char)); if (tmp == NULL) {
printf("Memory allocation failed.\n"); }
// 元の文字列をコピー
for (i = 0; i < len; i++) {
tmp[i] = s[i];
}
// 逆順に文字を追加
for (i = 0; i < len; i++) {
tmp[len + i] = s[len - 1 - i];
}
// 終端文字を追加
tmp[2 * len] = '\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+OBleOCkuWPluW+l+OBmeOCi+mWouaVsAppbnQgbXlTdHJsZW4oY2hhciBzW10pIHsKICAgIGludCBpOwogICAgZm9yIChpID0gMDsgc1tpXSAhPSAnXDAnOyBpKyspOwogICAgcmV0dXJuIGk7Cn0KCi8vIOWFpeWKm+aWh+Wtl+WIl+OCkuWbnuaWh+OBq+OBmeOCi+mWouaVsApjaGFyICpzZXRQYWxpbmRyb21lKGNoYXIgc1tdKSB7CiAgICBpbnQgbGVuID0gbXlTdHJsZW4ocyk7IC8vIOWFpeWKm+aWh+Wtl+WIl+OBrumVt+OBleOCkuWPluW+lwogICAgaW50IGk7CiAgICBjaGFyICp0bXA7CgogICAgLy8g5YuV55qE44Gr44Oh44Oi44Oq44KS56K65L+dCiAgICB0bXAgPSAoY2hhciAqKW1hbGxvYygoMiAqIGxlbiArIDEpICogc2l6ZW9mKGNoYXIpKTsKICAgIGlmICh0bXAgPT0gTlVMTCkgewogICAgICAgIHByaW50ZigiTWVtb3J5IGFsbG9jYXRpb24gZmFpbGVkLlxuIik7CiAgICAgICAgZXhpdCgxKTsKICAgIH0KCiAgICAvLyDlhYPjga7mloflrZfliJfjgpLjgrPjg5Tjg7wKICAgIGZvciAoaSA9IDA7IGkgPCBsZW47IGkrKykgewogICAgICAgIHRtcFtpXSA9IHNbaV07CiAgICB9CgogICAgLy8g6YCG6aCG44Gr5paH5a2X44KS6L+95YqgCiAgICBmb3IgKGkgPSAwOyBpIDwgbGVuOyBpKyspIHsKICAgICAgICB0bXBbbGVuICsgaV0gPSBzW2xlbiAtIDEgLSBpXTsKICAgIH0KCiAgICAvLyDntYLnq6/mloflrZfjgpLov73liqAKICAgIHRtcFsyICogbGVuXSA9ICdcMCc7CgogICAgcmV0dXJuIHRtcDsKfQoKLy8g44Oh44Kk44Oz6Zai5pWwCmludCBtYWluKCkgewogICAgY2hhciBueXVyeW9rdVsxMDI0XTsgLy8g5YWl5Yqb55So44OQ44OD44OV44KhCiAgICBjaGFyICprYWlidW47ICAgICAgICAvLyDlm57mlofjgpLlj5fjgZHlj5bjgosKICAgIHNjYW5mKCIlcyIsIG55dXJ5b2t1KTsKICAgIGthaWJ1biA9IHNldFBhbGluZHJvbWUobnl1cnlva3UpOwogICAgcHJpbnRmKCIlc1xuICAtPiAlc1xuIiwgbnl1cnlva3UsIGthaWJ1bik7CiAgICBmcmVlKGthaWJ1bik7IC8vIOWLleeahOODoeODouODquOCkuino+aUvgogICAgcmV0dXJuIDA7Cn0K