sorting a single string that is separted by spaces

given string is
abc abaa banana ban caba
answer should be
abc ab ban ban cab
i need to sort and delete the duplicates in the can i approach the problem in c++

There are two cases:

Case 1:
we know the number of space-separated strings.

for this

vector<string> v;
string a;
for(int i = 0; i < n; ++i){
  cin >> a;

Case 2: We don’t know n

vector<string> v;
string a,b;
istringstream ss(a);
while(getline(ss,b,' ')){

Now All we have to do is sort the vector



Advice: if you don’t know sorting algorithms, learn them thoroughly. You will need them. Do not be dependent on sort.

1 Like