#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 size = myStrlen(s);
// メモリを確保: 回文の長さは 2 * size + 1 (null terminator)
char *tmp
= (char *)malloc(sizeof(char) * (2 * size
+ 1));
if (tmp == NULL) {
// メモリ確保に失敗した場合はエラーメッセージを出して終了
printf("Memory allocation failed.\n"); }
// 元の文字列を tmp にコピー
for (int i = 0; i < size; i++) {
tmp[i] = s[i];
}
for (int j =size; j > 0; j--) {
tmp[j] = s[j];
}
tmp[2 * size] = '\0';
return tmp;
}
int main() {
int i;
char nyuryoku[1024]; // 入力
char *kaibun; // 回文を受け取る
// 入力を受け取る
// 回文を作成
kaibun = setPalindrome(nyuryoku);
// 結果を表示
printf("%s\n -> %s\n", nyuryoku
, kaibun
);
// メモリの解放
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBteVN0cmxlbihjaGFyIHNbXSkgewogICAgaW50IGk7CiAgICBmb3IgKGkgPSAwOyBzW2ldICE9ICdcMCc7IGkrKyk7CiAgICByZXR1cm4gaTsKfQoKLy8g5Zue5paH44KS5L2c5oiQ44GZ44KL6Zai5pWwCmNoYXIgKnNldFBhbGluZHJvbWUoY2hhciBzW10pIHsKICAgIGludCBzaXplID0gbXlTdHJsZW4ocyk7CiAgICAKICAgIC8vIOODoeODouODquOCkueiuuS/nTog5Zue5paH44Gu6ZW344GV44GvIDIgKiBzaXplICsgMSAobnVsbCB0ZXJtaW5hdG9yKQogICAgY2hhciAqdG1wID0gKGNoYXIgKiltYWxsb2Moc2l6ZW9mKGNoYXIpICogKDIgKiBzaXplICsgMSkpOwogICAgCiAgICBpZiAodG1wID09IE5VTEwpIHsKICAgICAgICAvLyDjg6Hjg6Ljg6rnorrkv53jgavlpLHmlZfjgZfjgZ/loLTlkIjjga/jgqjjg6njg7zjg6Hjg4Pjgrvjg7zjgrjjgpLlh7rjgZfjgabntYLkuoYKICAgICAgICBwcmludGYoIk1lbW9yeSBhbGxvY2F0aW9uIGZhaWxlZC5cbiIpOwogICAgICAgIGV4aXQoMSk7CiAgICB9CgogICAgLy8g5YWD44Gu5paH5a2X5YiX44KSIHRtcCDjgavjgrPjg5Tjg7wKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc2l6ZTsgaSsrKSB7CiAgICAgICAgdG1wW2ldID0gc1tpXTsKICAgIH0KICAgIGZvciAoaW50IGogPXNpemU7IGogPiAwOyBqLS0pIHsKICAgICAgICB0bXBbal0gPSBzW2pdOwogICAgfQogICAgdG1wWzIgKiBzaXplXSA9ICdcMCc7CgogICAgcmV0dXJuIHRtcDsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgaTsKICAgIGNoYXIgbnl1cnlva3VbMTAyNF07IC8vIOWFpeWKmwogICAgY2hhciAqa2FpYnVuOyAgICAgICAgLy8g5Zue5paH44KS5Y+X44GR5Y+W44KLCgogICAgLy8g5YWl5Yqb44KS5Y+X44GR5Y+W44KLCiAgICBzY2FuZigiJXMiLCBueXVyeW9rdSk7CgogICAgLy8g5Zue5paH44KS5L2c5oiQCiAgICBrYWlidW4gPSBzZXRQYWxpbmRyb21lKG55dXJ5b2t1KTsKCiAgICAvLyDntZDmnpzjgpLooajnpLoKICAgIHByaW50ZigiJXNcbiAgLT4gJXNcbiIsIG55dXJ5b2t1LCBrYWlidW4pOwoKICAgIC8vIOODoeODouODquOBruino+aUvgogICAgZnJlZShrYWlidW4pOwoKICAgIHJldHVybiAwOwp9Cg==