Задача
Дан текст. Группы символов, разделенные пробелами (одним или несколькими) и не содержащие пробелы внутри себя, будем называть их словами.В тех словах, которые оканчиваются сочитанием букв [latex]-ing[/latex] заменить это окончание на [latex]-ed[/latex].
Тест
Для проверки я введу первый абзац и второй, так как могут возникнуть вопросы, связанные с считыванием [latex]getline()[/latex] до первого переноса строки.
Вводимый текст | Выводимый текст |
Alice was beginning to get very tired of sitting
by her sister on the bank, and of having nothing to do: once or twice she had peened into the book her sister was reading, but it had no pictures or con- versations in it, and what is the use of a book,’ througt Alice ‘without pictures or conversation?’ So she was considering in her own .. |
Alice was begined to get very tired of sitted
by her sister on the bank, and of haved nothed to do: once or twice she had peened into the book her sister was readed , but it had no pictures or con- versations in it, and what is the use of a book,’ througt Alice ‘without pictures or conversation?’ So she was considered in her own … |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <algorithm> using namespace std; int main () { string s; while(getline (cin, s)){ for (unsigned int i = 0; i < s.length(); i++) { if (s[i]=='i' && s[i+1]=='n' && s[i+2]=='g') {s[i]='e'; s[i+1]='d'; s[i+2]=' ';} } cout << s; } return 0; } |
Ссылка на программу: http://ideone.com/S5ZlwZ
Решение
Вводим строку в переменную [latex]s[/latex].В цикле пишем условие, что пока будут встречаться буквосочетания [latex]-ing[/latex] заменить их на [latex]-ed[/latex] и выводим уже обработанный текст.
Заменять нужно все ing-овый ОКОНЧАНИЯ, а не три буквы ing в любом месте слова.