fork(2) download
  1. #define __STDC_WANT_LIB_EXT1__ 1
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #define BUF_LEN 100 // Length of input buffer
  7. #define COUNT 5 // Initial number of strings
  8. int main(void)
  9. {
  10. char buf[BUF_LEN]; // Input buffer
  11. size_t str_count = 0; // Current string count
  12. size_t capacity = COUNT; // Current maximum number of strings
  13. char **pS = calloc(capacity, sizeof(char*)); // Pointers to strings
  14. char** psTemp = NULL; // Temporary pointer to pointer to char
  15. char* pTemp = NULL; // Temporary pointer to char
  16. size_t str_len = 0; // Length of a string
  17. bool sorted = false; // Indicated when strings are sorted
  18. printf("Enter strings to be sorted, one per line. Press Enter to end:\n");
  19. // Read in all the strings
  20. char *ptr = NULL;
  21. while (true)
  22. {
  23. ptr = fgets(buf, BUF_LEN, stdin);
  24. if (!ptr) // Check for read error
  25. {
  26. printf("Error reading string.\n");
  27. free(pS);
  28. pS = NULL;
  29. return 1;
  30. }
  31. if (*ptr == '\n') break; // Empty line check
  32. if (str_count == capacity)
  33. {
  34. capacity += capacity / 4; // Increase capacity by 25%
  35. if (!(psTemp = realloc(pS, capacity))) return 1;
  36. pS = psTemp;
  37. }
  38. str_len = strnlen_s(buf, BUF_LEN) + 1;
  39. if (!(pS[str_count] = malloc(str_len))) return 2;
  40. strcpy_s(pS[str_count++], str_len, buf);
  41. }
  42. // Sort the strings in ascending order
  43. while (!sorted)
  44. {
  45. sorted = true;
  46. for (size_t i = 0; i < str_count - 1; ++i)
  47. {
  48. if (strcmp(pS[i], pS[i + 1]) > 0)
  49. {
  50. sorted = false; // We were out of order so. . .
  51. pTemp = pS[i]; // swap pointers pS[i]. . .
  52. pS[i] = pS[i + 1]; // and. . .
  53. pS[i + 1] = pTemp; // pS[i + 1]
  54. }
  55. }
  56. }
  57. // Output the sorted strings
  58. printf("Your input sorted in ascending sequence is:\n\n");
  59. for (size_t i = 0; i < str_count; ++i)
  60. {
  61. printf("%s", pS[i]);
  62. free(pS[i]); // Release memory for the word
  63. pS[i] = NULL; // Reset the pointer
  64. }
  65.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘main’:
prog.c:38:3: warning: implicit declaration of function ‘strnlen_s’ [-Wimplicit-function-declaration]
   str_len = strnlen_s(buf, BUF_LEN) + 1;
   ^
prog.c:40:3: warning: implicit declaration of function ‘strcpy_s’ [-Wimplicit-function-declaration]
   strcpy_s(pS[str_count++], str_len, buf);
   ^
prog.c:46:3: error: ‘for’ loop initial declarations are only allowed in C99 mode
   for (size_t i = 0; i < str_count - 1; ++i)
   ^
prog.c:46:3: note: use option -std=c99 or -std=gnu99 to compile your code
prog.c:59:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
  for (size_t i = 0; i < str_count; ++i)
  ^
prog.c:64:2: error: expected declaration or statement at end of input
  }
  ^
prog.c:64:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^
stdout
Standard output is empty