#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int myStrlen(char s[]){
int i;
for(i=0;s[i]!='\0';i++);
return i;
}
char *setPalindrome(char s[]){
int len = myStrlen(s);
if (len == 0) {
if (tmp) tmp[0] = '\0';
return tmp;
}
int i, j;
int suffix_palindrome_start_index = 0;
for (i = 0; i < len; i++) {
int is_palindrome = 1;
for (j = 0; j < (len - i) / 2; j++) {
if (s[i + j] != s[len - 1 - j]) {
is_palindrome = 0;
break;
}
}
if (is_palindrome) {
suffix_palindrome_start_index = i;
break;
}
}
int chars_to_add = suffix_palindrome_start_index;
char *result
= (char*)malloc(len
+ chars_to_add
+ 1); if (result == NULL) {
return NULL;
}
int current_result_len = len;
for (i = 0; i < chars_to_add; i++) {
char char_to_append = s[chars_to_add - 1 - i];
result[current_result_len + i] = char_to_append;
}
result[len + chars_to_add] = '\0';
return result;
}
int main(){
int i;
char nyuryoku[1024];
char *kaibun;
kaibun = setPalindrome(nyuryoku);
printf("%s -> %s\n", nyuryoku
, kaibun
); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKaW50IG15U3RybGVuKGNoYXIgc1tdKXsKCWludCBpOwoJZm9yKGk9MDtzW2ldIT0nXDAnO2krKyk7CglyZXR1cm4gaTsKfQoKY2hhciAqc2V0UGFsaW5kcm9tZShjaGFyIHNbXSl7CglpbnQgbGVuID0gbXlTdHJsZW4ocyk7CglpZiAobGVuID09IDApIHsKCQljaGFyICp0bXAgPSAoY2hhciopbWFsbG9jKDEpOwoJCWlmICh0bXApIHRtcFswXSA9ICdcMCc7CgkJcmV0dXJuIHRtcDsKCX0KCQoJaW50IGksIGo7CglpbnQgc3VmZml4X3BhbGluZHJvbWVfc3RhcnRfaW5kZXggPSAwOwoJZm9yIChpID0gMDsgaSA8IGxlbjsgaSsrKSB7CgkJaW50IGlzX3BhbGluZHJvbWUgPSAxOwoJCWZvciAoaiA9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