fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 2000;
  6.  
  7. bool isLetter(char c) {
  8. return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  9. }
  10.  
  11. void changeLetter(char text[], const int firstPos, const int numOfWords, const int numOfLetters) {
  12. if (numOfWords <= numOfLetters) {
  13. text[firstPos + numOfWords - 1] = 'a';
  14. } else {
  15. text[firstPos] = 'a';
  16. }
  17. }
  18.  
  19. void modifyText(char text[], int &numOfWords) {
  20. int firstPos = 0, numOfLetters = 0, textLength = strlen(text);
  21. bool prevLetter = false;
  22. for (int i = 0; i <= textLength; ++i) {
  23. if (isLetter(text[i])) {
  24. ++numOfLetters;
  25. if (prevLetter == false) {
  26. ++numOfWords;
  27. firstPos = i;
  28. }
  29. prevLetter = true;
  30. } else if (prevLetter) {
  31. changeLetter(text, firstPos, numOfWords, numOfLetters);
  32. numOfLetters = 0;
  33. prevLetter = false;
  34. }
  35. }
  36. }
  37.  
  38. int main() {
  39. char text[MAX_SIZE + 1];
  40. int numOfWords = 0;
  41. while (cin.getline(text, MAX_SIZE + 1)) {
  42. modifyText(text, numOfWords);
  43. cout << text << '\n';
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 5328KB
stdin
B b b b b b
C c c c c
stdout
a a a a a a
a a a a a