Splitting strings

Hi, I have this code. There is a string with three sentences and I need to sort them (with dot '.' as separator) and store them somehow for later usage (bubble sort). I achieved to separate the first sentence but have no idea how to store it and what to do next. Thank you for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <sstream>
#include <vector>
#include <string>
using namespace std;

const char text[] = "This sentence is first. And this\n" 
    "is second one. End.\n"; 

void sorting(void)
{
  std::string text2 = text;
  int i;
  string string1 = "";
  for (i = 0; i < strlen(text); i++){
      if (text[i] == '.'){
            break;}
      string1 += text[i]; 
      
}
string1 += '.';  
cout << string1 << endl;  
}

int main(int argc, char *argv[])
{
sorting();
getchar();
return 0;
}
Is this what you want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sorting(void) {
	std::string text2 = text;
	int i;
	string string1 = "";
	vector<string> str;
	for (i = 0; i < strlen(text); i++) {
		if (text[i] == '.') {
			str.push_back(string1);
			string1.erase();
		}
		string1 += text[i];
	}
	for (vector<string>::iterator it = str.begin(); it != str.end(); ++it) {
		std::cout << ' ' << *it;
		std::cout << '\n';
	}
}


This sentence is first
 . And this
is second one
 . End

Last edited on
Thank you very much for your reply! Your code works perfectly.
Topic archived. No new replies allowed.