#include <stdio.h>
#include <stdlib.h>
int myStrlen(char s[]){
int i;
for(i=0; s[i] != '\0'; i++);
return i;
}
// 部分文字列が回文かどうかを判定
int isPalindrome(char s[], int start, int end){
while(start < end){
if(s[start] != s[end]) return 0;
start++;
end--;
}
return 1;
}
char *setPalindrome(char s[]){
int len = myStrlen(s);
int addIndex = 0;
for(int i = 0; i < len; i++){
if(isPalindrome(s, i, len-1)){
addIndex = i;
break;
}
}
int addLen = addIndex;
int newLen = len + addLen;
char *tmp
= (char *)malloc((newLen
+ 1) * sizeof(char)); if(tmp == NULL){
printf("Memory allocation failed\n"); }
for(int i = 0; i < len; i++){
tmp[i] = s[i];
}
for(int i = 0; i < addLen; i++){
tmp[len + i] = s[addIndex - 1 - i];
}
tmp[newLen] = '\0';
return tmp;
}
int main(){
char nyuryoku[1024];
char *kaibun;
kaibun = setPalindrome(nyuryoku);
printf("%s\n -> %s\n", nyuryoku
, kaibun
); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBteVN0cmxlbihjaGFyIHNbXSl7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7IHNbaV0gIT0gJ1wwJzsgaSsrKTsKICAgIHJldHVybiBpOwp9CgovLyDpg6jliIbmloflrZfliJfjgYzlm57mlofjgYvjganjgYbjgYvjgpLliKTlrpoKaW50IGlzUGFsaW5kcm9tZShjaGFyIHNbXSwgaW50IHN0YXJ0LCBpbnQgZW5kKXsKICAgIHdoaWxlKHN0YXJ0IDwgZW5kKXsKICAgICAgICBpZihzW3N0YXJ0XSAhPSBzW2VuZF0pIHJldHVybiAwOwogICAgICAgIHN0YXJ0Kys7CiAgICAgICAgZW5kLS07CiAgICB9CiAgICByZXR1cm4gMTsKfQoKY2hhciAqc2V0UGFsaW5kcm9tZShjaGFyIHNbXSl7CiAgICBpbnQgbGVuID0gbXlTdHJsZW4ocyk7CiAgICBpbnQgYWRkSW5kZXggPSAwOwoKICAgIGZvcihpb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