fork download
  1. #include <ctype.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. bool is_identifier(const char *word) {
  7. if (!((word[0] >= 'a' && word[0] <= 'z') ||
  8. (word[0] >= 'A' && word[0] <= 'Z') || word[0] == '_')) {
  9. return 0;
  10. }
  11. for (int i = 1; i < strlen(word); i++) {
  12. if (!((word[i] >= 'a' && word[i] <= 'z') ||
  13. (word[i] >= 'A' && word[i] <= 'Z') ||
  14. (word[i] >= '0' && word[i] <= '9') || word[i] == '_')) {
  15. return 0;
  16. }
  17. }
  18. return 1;
  19. }
  20.  
  21. void reverse_non_identifiers(char *line) {
  22. char *start = line, *end;
  23. while (*start != '\0') {
  24. while (*start == ' ') {
  25. start++;
  26. }
  27. end = start;
  28. while (*end != ' ' && *end != '\0') {
  29. end++;
  30. }
  31. if (!is_identifier(start)) {
  32. int len = end - start;
  33. for (int i = 0; i < len / 2; i++) {
  34. char temp = start[i];
  35. start[i] = start[len - i - 1];
  36. start[len - i - 1] = temp;
  37. }
  38. }
  39. start = end;
  40. }
  41. }
  42.  
  43. int main() {
  44. char line[10000000];
  45. while (fgets(line, sizeof(line), stdin)) {
  46. char *newline = strtok(line, "\n");
  47. int len = strlen(newline);
  48. char *copy = malloc(len + 1);
  49. strcpy(copy, newline);
  50. reverse_non_identifiers(copy);
  51. puts(copy);
  52. free(copy);
  53. }
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5392KB
stdin
Dato25 has 2 c-ats and 3   _dogs.   . 
stdout
52otaD sah 2 sta-c dna 3   .sgod_   .