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,size;
  15. count = myStrlen(s);
  16. size = count*2-1;
  17. tmp = (int *)malloc(sizeof(int)*size);
  18. if(tmp==NULL){
  19. printf("ERROR\n");
  20. return 0;
  21. }
  22. for(int i=0; i<count; i++){
  23. tmp[i] = s[i];
  24. }
  25. for(int i=0; i<count-1; i++){
  26. tmp[size-1-i] = s[i];
  27. }
  28. tmp[size]='\0';
  29. return tmp;
  30. }
  31.  
  32.  
  33. //メイン関数はいじる必要はありません
  34. int main(){
  35. int i;
  36. char nyuryoku[1024]; //入力
  37. char *kaibun; //回文を受け取る
  38. scanf("%s",nyuryoku);
  39. kaibun = setPalindrome(nyuryoku); //ポインタ渡し
  40. printf("%s\n -> %s\n",nyuryoku,kaibun);
  41. free(kaibun);
  42. return 0;
  43. }
  44.  
Success #stdin #stdout 0.01s 5272KB
stdin
abcd
stdout
abcd
  -> abcdcba