fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int myStrlen(char s[]) {
  5. int i;
  6. for (i = 0; s[i] != '\0'; i++);
  7. return i;
  8. }
  9.  
  10. char *setPalindrome(char s[]) {
  11. int len = myStrlen(s);
  12. int new_len = len * 2;
  13. char *tmp = (char *)malloc((new_len + 1) * sizeof(char));
  14.  
  15. if (tmp == NULL) {
  16. printf("Memory allocation failed\n");
  17. exit(1);
  18. }
  19.  
  20. for (int i = 0; i < len; i++) {
  21. tmp[i] = s[i];
  22. }
  23.  
  24. for (int i = 0; i < len; i++) {
  25. tmp[len + i] = s[len - 1 - i];
  26. }
  27.  
  28. tmp[new_len] = '\0';
  29. return tmp;
  30. }
  31.  
  32. int main() {
  33. char nyuryoku[1024];
  34. char *kaibun;
  35. scanf("%s", nyuryoku);
  36. kaibun = setPalindrome(nyuryoku);
  37. printf("%s\n -> %s\n", nyuryoku, kaibun);
  38. free(kaibun);
  39. return 0;
  40. }
  41.  
Success #stdin #stdout 0s 5280KB
stdin
abcd
stdout
abcd
  -> abcddcba