fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SIZE 30
  6.  
  7. char* invertStr(const char *source)
  8. {
  9. int size = strlen(source);
  10.  
  11. char *inverted = malloc(sizeof(source) * (size + 1));
  12.  
  13. int count = size;
  14.  
  15. for (int i = 0; i < size; i++)
  16. {
  17. inverted[i] = source[count];
  18. count--;
  19. }
  20. inverted[size + 1] = ('\0');
  21.  
  22. return inverted;
  23. }
  24.  
  25. int main(void)
  26. {
  27. char str[SIZE];
  28. char str2[SIZE];
  29.  
  30. scanf("%29s", str);
  31.  
  32. char *inverted = invertStr(str);
  33.  
  34. if (inverted == 0)
  35. {
  36. printf("NULL Pointer. Memory alocation error");
  37.  
  38. return -1;
  39. }
  40.  
  41. strcpy(str2, inverted);
  42.  
  43. printf("%s | %s\n", str, str2);
  44.  
  45. free(inverted);
  46.  
  47. return EXIT_SUCCESS;
  48. }
Success #stdin #stdout 0s 4788KB
stdin
Stack
stdout
Stack |