fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. #define MARKUP_LEN 7
  7.  
  8. void place_char(char *string, char toPlace, int index, bool reg) {
  9. switch (reg) {
  10. case true:
  11. string[index] = toPlace;
  12. break;
  13. case false:
  14. string[index] = '[';
  15. string[index + 1] = toPlace;
  16. string[index + 2] = ']';
  17. break;
  18. default:
  19. fprintf(stderr, "error\n");
  20. }
  21. }
  22.  
  23. int main(void) {
  24. char buffer[1025];
  25. while (fgets(buffer, 1025, stdin) != NULL) {
  26. if (buffer[strlen(buffer) - 1] == '\n') {
  27. buffer[strlen(buffer) - 1] = '\0';
  28. }
  29. size_t orig_str_len = strlen(buffer);
  30. char *new_string = calloc(orig_str_len * 4 + 1, sizeof(char));
  31. short markup = 1;
  32. int i = 0;
  33. unsigned int index;
  34. for (index = 0; index < orig_str_len; index++) {
  35. if (buffer[index] != ' ') {
  36. place_char(new_string, '0' + markup, i, false);
  37. i += 3;
  38. } else {
  39. markup--;
  40. }
  41. place_char(new_string, buffer[index], i, true);
  42. i += 1;
  43. markup++;
  44. if (markup > MARKUP_LEN)
  45. markup = 1;
  46. }
  47. printf("\tOriginal: %s\n\tRainbow: %s\n", buffer, new_string);
  48. free(new_string);
  49. }
  50. return EXIT_SUCCESS;
  51. }
Success #stdin #stdout 0s 9432KB
stdin
Hello!?
Welcome to the server!
stdout
	Original: Hello!?
	Rainbow: [1]H[2]e[3]l[4]l[5]o[6]![7]?
	Original: Welcome to the server!
	Rainbow: [1]W[2]e[3]l[4]c[5]o[6]m[7]e [1]t[2]o [3]t[4]h[5]e [6]s[7]e[1]r[2]v[3]e[4]r[5]!