#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);
// 回文を格納するためのメモリを確保
char *tmp
= (char*)malloc(sizeof(char) * (len
* 2 + 1));
if (tmp == NULL) { // メモリ確保が失敗した場合
printf("Memory allocation failed.\n"); return NULL;
}
// 元の文字列をtmpにコピー
for (int i = 0; i < len; i++) {
tmp[i] = s[i];
}
// 反転した文字列をtmpの後ろに追加
for (int i = 0; i < len; i++) {
tmp[len + i] = s[len - 1 - i];
}
tmp[len * 2] = '\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+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBteVN0cmxlbihjaGFyIHNbXSl7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7IHNbaV0hPSdcMCc7IGkrKyk7CiAgICByZXR1cm4gaTsKfQoKLy8g5Zue5paH44KS55Sf5oiQ44GZ44KL6Zai5pWwCmNoYXIgKnNldFBhbGluZHJvbWUoY2hhciBzW10pewogICAgaW50IGxlbiA9IG15U3RybGVuKHMpOwogICAgLy8g5Zue5paH44KS5qC857SN44GZ44KL44Gf44KB44Gu44Oh44Oi44Oq44KS56K65L+dCiAgICBjaGFyICp0bXAgPSAoY2hhciopbWFsbG9jKHNpemVvZihjaGFyKSAqIChsZW4gKiAyICsgMSkpOwoKICAgIGlmICh0bXAgPT0gTlVMTCkgeyAgLy8g44Oh44Oi44Oq56K65L+d44GM5aSx5pWX44GX44Gf5aC05ZCICiAgICAgICAgcHJpbnRmKCJNZW1vcnkgYWxsb2NhdGlvbiBmYWlsZWQuXG4iKTsKICAgICAgICByZXR1cm4gTlVMTDsKICAgIH0KCi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