I think my decision can help:
#include <iostream> #include <string> #include <fstream> using namespace std; int main() { string currentSentence = ""; char ch; int sentenceCounter = 0; ifstream infile("test.txt"); // your file while(infile) { infile.get(ch); /* Если текущий символ - точка, то наше предложение утвердительное Если счетчик меньше 2, значит увеличиваем счетчик и идем дальше */ if((ch == '.') &&(sentenceCounter < 2)) { sentenceCounter++; continue; } /* Если мы уже встретили две точки в файле, то сейчас будет третье предложение. Нужно учесть, что могут попадаться ! или ?. Если они попадаются, то обнуляем строку и считываем файл дальше пока не дойдем до конца или до точки. */ if(sentenceCounter == 2) { if((ch != '!') && (ch != '?') && (ch != '.')) { currentSentence+=ch; continue; } if(ch == '.') { cout<<currentSentence; break; } //очищаем, если встретился восклицательный или вопросительный знак if((ch == '!') || (ch == '?')) { currentSentence.erase(); continue; } } currentSentence += ch; } return 0; }
The truth here is that the sentence is not marked with asterisks, it is displayed in the console for ease of example. I think it will not be difficult to rewrite the proposal for the selection of stars.
Tested using test1 . test2 ? test3 ! test4. test5? test6. test7? as an example test1 . test2 ? test3 ! test4. test5? test6. test7? test1 . test2 ? test3 ! test4. test5? test6. test7? , correctly displays test6 .
For such punctuation marks as a dot, exclamation point and question mark, everything works. You should add a program to recognize the ellipsis. And do not forget about the support of Russian characters.