fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int myStrlen(char s[]){
  6. int i;
  7. for(i=0;s[i]!='\0';i++);
  8. return i;
  9. }
  10.  
  11. char *setPalindrome(char s[]){
  12. int len = myStrlen(s);
  13. if (len == 0) {
  14. char *tmp = (char*)malloc(1);
  15. if (tmp) tmp[0] = '\0';
  16. return tmp;
  17. }
  18.  
  19. int i, j;
  20. int suffix_palindrome_start_index = 0;
  21. for (i = 0; i < len; i++) {
  22. int is_palindrome = 1;
  23. for (j = 0; j < (len - i) / 2; j++) {
  24. if (s[i + j] != s[len - 1 - j]) {
  25. is_palindrome = 0;
  26. break;
  27. }
  28. }
  29.  
  30. if (is_palindrome) {
  31. suffix_palindrome_start_index = i;
  32. break;
  33. }
  34. }
  35.  
  36. int chars_to_add = suffix_palindrome_start_index;
  37. char *result = (char*)malloc(len + chars_to_add + 1);
  38. if (result == NULL) {
  39. return NULL;
  40. }
  41.  
  42. strcpy(result, s);
  43.  
  44. int current_result_len = len;
  45. for (i = 0; i < chars_to_add; i++) {
  46. char char_to_append = s[chars_to_add - 1 - i];
  47. result[current_result_len + i] = char_to_append;
  48. }
  49.  
  50. result[len + chars_to_add] = '\0';
  51. return result;
  52. }
  53.  
  54. int main(){
  55. int i;
  56.  
  57. char nyuryoku[1024];
  58. char *kaibun;
  59. scanf("%s", nyuryoku);
  60. kaibun = setPalindrome(nyuryoku);
  61. printf("%s -> %s\n", nyuryoku, kaibun);
  62. free(kaibun);
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5320KB
stdin
abab
stdout
abab -> ababa