#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+dCiAgICB0bXAgPSAoY2hhciAqKW1hbGxvYygoMiAqIGxlbiArIDEpICogc2l6ZW9mKGNoYXIpKTsKICAgIGlmICh0bXAgPT0gTlVMTCkgewogICAgICAgIHByaW50ZigiTW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