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. {
  12. int len = myStrlen(s);
  13. int total_len = 2 * len;
  14. char *tmp = (char *)malloc((total_len + 1) * sizeof(char));
  15.  
  16. if (tmp == NULL)
  17. {
  18. fprintf(stderr, "Memory allocation failed\n");
  19. return NULL;
  20. }
  21.  
  22.  
  23. for (int i = 0; i < len; i++)
  24. {
  25. tmp[i] = s[i];
  26. tmp[total_len - 1 - i] = s[i];
  27. }
  28. tmp[total_len] = '\0';
  29.  
  30. return tmp;
  31. }
  32.  
  33. int main(){
  34. int i;
  35. char nyuryoku[1024]; // 入力
  36. char *kaibun; // 回文を受け取る
  37. scanf("%s", nyuryoku);
  38. kaibun = setPalindrome(nyuryoku);
  39. printf("%s\n -> %s\n", nyuryoku, kaibun);
  40. free(kaibun);
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0.01s 5292KB
stdin
abcd
stdout
abcd
  -> abcddcba