fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 2000;
  6.  
  7. bool isLetter(char a) {
  8. return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
  9. }
  10.  
  11. void changeWord(char a[]) {
  12. int n = strlen(a);
  13. char b[MAX_SIZE];
  14. if (n % 2 == 0) {
  15. int index = n / 2;
  16. for (int i = 0; i < n; ++i) {
  17. b[i] = a[index];
  18. ++index;
  19. if (index == n) {
  20. index = 0;
  21. }
  22. }
  23. } else if (n % 2 != 0) {
  24. int index = n / 2 + 1;
  25. for (int i = 0; i < n; ++i) {
  26. b[i] = a[index];
  27. ++index;
  28. if (index == n) {
  29. index = 0;
  30. ++i;
  31. b[i] = a[n / 2];
  32. }
  33. }
  34. }
  35. b[n] = '\0'; // Terminator de șir
  36. strcpy(a, b);
  37. }
  38.  
  39. void makeInversRow(char a[]) {
  40. int n = strlen(a);
  41. int j = 0;
  42. char word[MAX_SIZE];
  43. bool inWord = false;
  44.  
  45. for (int i = 0; i < n; ++i) {
  46. if (isLetter(a[i])) {
  47. if (!inWord) {
  48. j = 0;
  49. inWord = true;
  50. }
  51. word[j++] = a[i];
  52. } else {
  53. if (inWord) {
  54. word[j] = '\0';
  55. changeWord(word);
  56. cout << word;
  57. inWord = false;
  58. }
  59. cout << a[i];
  60. }
  61. }
  62.  
  63. if (inWord) {
  64. word[j] = '\0';
  65. changeWord(word);
  66. cout << word;
  67. }
  68. }
  69.  
  70. int main() {
  71. char row[MAX_SIZE];
  72. while (cin.getline(row, MAX_SIZE)) {
  73. makeInversRow(row);
  74. cout << endl; // Adaugă o linie nouă după fiecare linie procesată
  75. }
  76. return 0;
  77. }
Success #stdin #stdout 0.01s 5284KB
stdin
Line one
Line two
Line three



stdout
neLi eno
neLi owt
neLi eerth