fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. char* setPalindrome(char *s) {
  6. int len = strlen(s);
  7.  
  8.  
  9. char *pal = (char*)malloc(sizeof(char) * (len * 2 + 1));
  10. if (pal == NULL) return NULL;
  11.  
  12.  
  13. for (int i = 0; i < len; i++) {
  14. pal[i] = s[i];
  15. }
  16.  
  17.  
  18. for (int i = 0; i < len; i++) {
  19. pal[len + i] = s[len - 1 - i];
  20. }
  21.  
  22. pal[len * 2] = '\0';
  23.  
  24. return pal;
  25. }
  26.  
  27. int main(void) {
  28. char s[100];
  29.  
  30. printf("文字列を入力:");
  31. scanf("%99s", s);
  32.  
  33. char *result = setPalindrome(s);
  34. printf("回文:%s\n", result);
  35.  
  36. free(result);
  37.  
  38. return 0;
  39. }
  40.  
Success #stdin #stdout 0s 5288KB
stdin
あい
stdout
文字列を入力:回文:あい��め�