fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 1000;
  6.  
  7. bool isLetter(char c) {
  8. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  9. }
  10.  
  11. bool isBigLetter(char c) {
  12. return c >= 'A' && c <= 'Z';
  13. }
  14.  
  15. bool isSmallLetter(char c) {
  16. return c >= 'a' && c <= 'z';
  17. }
  18.  
  19. bool checkCaps(char text[]) {
  20. char word[100] = {0};
  21. strcpy(word, text);
  22. int n = strlen(word);
  23. for (int i = 0; i < n; ++i) {
  24. if (isBigLetter(word[i])) {
  25. word[i] = word[i] + ('a' - 'A');
  26. }
  27. }
  28. return (strcmp(word, "caps") == 0);
  29. }
  30.  
  31. void processWords(char text[], bool &capsLock) {
  32. int n = strlen(text), lettersCount = 0;
  33. bool isWord = false;
  34. for (int i = 0; i <= n; ++i) {
  35. if (isLetter(text[i])) {
  36. if (capsLock) {
  37. if (isSmallLetter(text[i])) {
  38. text[i] = text[i] - ('a' - 'A');
  39. }
  40. } else {
  41. if (isBigLetter(text[i])) {
  42. text[i] = text[i] + ('a' - 'A');
  43. }
  44. }
  45. isWord = true;
  46. ++lettersCount;
  47. } else if (isWord) {
  48. char temp = text[i];
  49. text[i] = 0;
  50. //cout << text + i - lettersCount << ", ";
  51. if (checkCaps(text + i - lettersCount)) {
  52. if (capsLock) {
  53. //cout << "MARE ";
  54. capsLock = false;
  55. } else {
  56. //cout << "MICA ";
  57. capsLock = true;
  58. }
  59. strcat(text + i - lettersCount, " ");
  60. strcat(text, text + i);
  61. }
  62. text[i] = temp;
  63. isWord = false;
  64. lettersCount = 0;
  65. }
  66. }
  67. }
  68.  
  69. int main() {
  70. char text[MAX_SIZE + 1] = {0};
  71. bool capsLock = false;
  72. while (cin.getline(text, MAX_SIZE + 1)) {
  73. processWords(text, capsLock);
  74. cout << text << "\n";
  75. }
  76. return 0;
  77. }
Success #stdin #stdout 0.01s 5272KB
stdin
Textul MIC caps acum este
mareEE Caps, nu-I asa?
stdout
textul mic caps  
MAREEE CAPS,