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. // 関数の中でtmpに対してmallocして
  11. // そこに回文を代入してreturnで返しましょう
  12. char *setPalindrome(char s[]){
  13. char *tmp;
  14. int count=0,a;
  15.  
  16. count = myStrlen(s);
  17.  
  18. a = count*2-1;
  19. tmp = (char *)malloc(sizeof(char)*(a+1));
  20.  
  21. if(tmp==NULL){
  22. printf("ERROR");
  23. return 0;
  24. }
  25.  
  26. for(int i=0; i<count; i++){
  27. tmp[i] = s[i];
  28. }
  29.  
  30. for(int i=count; i<a; i++){
  31. tmp[i] = s[a-1-i];
  32. }
  33.  
  34. tmp[a]='\0';
  35. return tmp;
  36. //以下に必要な宣言を含めて書いてください
  37. }
  38.  
  39.  
  40. //メイン関数はいじる必要はありません
  41. int main(){
  42. char nyuryoku[1024]; //入力
  43. char *kaibun; //回文を受け取る
  44. scanf("%s",nyuryoku);
  45. kaibun = setPalindrome(nyuryoku);
  46. printf("%s\n -> %s\n",nyuryoku,kaibun);
  47. free(kaibun);
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0.01s 5320KB
stdin
abcd
stdout
abcd
  -> abcdcba