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 numOfLetters, const int numOfWords) {
  12. if (numOfWords <= numOfLetters) {
  13. text[firstPos + numOfLetters - 1] = 'a';
  14. } else {
  15. text[firstPos] = 'a';
  16. }
  17. }
  18.  
  19. void modifyText(char text[], int &numOfWords) {
  20. int firstPos = 0, textLength = strlen(text);
  21. int numOfLetters = 0;
  22. bool prevLetter = false;
  23. for (int i = 0; i <= textLength; ++i) {
  24. if (isLetter(text[i])) {
  25. ++numOfLetters;
  26. if (prevLetter == false) {
  27. ++numOfWords;
  28. firstPos = i;
  29. }
  30. prevLetter = true;
  31. } else if (prevLetter) {
  32. changeLetter(text, firstPos, numOfLetters, numOfWords);
  33. numOfLetters = 0;
  34. prevLetter = false;
  35. }
  36. }
  37. }
  38.  
  39. int main() {
  40. char text[MAX_SIZE + 1];
  41. int numOfWords = 0;
  42. while (cin.getline(text, MAX_SIZE + 1)) {
  43. modifyText(text, numOfWords);
  44. cout << text << '\n';
  45. }
  46. return 0;
  47. }
Success #stdin #stdout 0s 5308KB
stdin
bcd
stdout
bca