problem with string break end with ".?!"

Hi. i have some problem with my prog.I need to break the string in txt.file.

like in the txt.file there is a string sentence:

How are you? I am fine.

How do i break the sentence to:

How are you?
I am fine.
Last edited on
1
2
3
4
5
6
7
8
9
getline(ifstream, str_var);

size_t pos = str_var.find("?");

string t1;
string t2;

t1 = str_var.substr(0, pos);
t2 = str_var.substr( pos +2 );


You can expand upon this example.
Do i still need to append? and do i get the string break. i try cannot only the first line break.
how are you?
I am fine. What about you? I fine too!

the txt file is :

How are you? I am fine. What about you? I fine too!

How are you?
I am fine.
what about you?
i fine too!

//find file and word
cin >> file name;
cin >> searchWord; // cin you

the output display:
how are you?
what about you?
how are you?
what about you?



while (!fin.eof())
{

getline(fin,line);

for(int i=0; i < line.length(); i++)
line[i] = tolower(line[i]);

for(int i=0; i < wordSearch.length(); i++)
wordSearch[i] = tolower(wordSearch[i]);

line.append(str1, 0, line.length()); // join the sentence to a line

found = line.find(wordSearch); // search for cin words
pos = line.find_first_of(".!?");

if ((found!=string::npos) &&( pos!=string::npos))
{

str3 = line.substr (0,pos+1);
str2 = line.substr(pos+1);

cout <<str3<< '\n' << str2 << endl;

}

}
Last edited on
I think you need to loop the string. Find punctuation operators and then create a substring. Then push the substring back in a list or vector and then output it.
erm . can show example? i cant get what u mean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
getline(infile, str);
vector<string> strings;
int pos = 0;

for(int i = 0; i < str.length(); ++i)
{

  if( (str[i] == '!') || (... all the punctuation operators)
  {
    
    strings.push_back(str.substr(pos, (i - (int)pos));
    pos = i + 1;

  }
}

for(int i = 0; i < (int)strings.size(); ++i)
{
  cout << strings[i] << endl;
}


I'm not sure if the substring method is correct. I didn't test this code. You can mess around with it.
The substr prototype is this:
substr( starting_position, number_of_characters_to_extract );

That is not what it is really but.. that's how it's used.
Topic archived. No new replies allowed.