Solved

sorting substrings

Posted on 2000-04-10
9
196 Views
Last Modified: 2010-04-02
I want to read a string from the file. Whole String: we, thank, you,

I want out put in the form of

Thank, you, we,
We, thank, you,
You, thank, we,

Note: Resulted strings are combination of their words and are arranged in alphabetical order i.e. t, w, y.


0
Comment
Question by:redgreenred
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2700732
Again homework.
0
 
LVL 9

Accepted Solution

by:
jasonclarke earned 50 total points
ID: 2700771
#pragma warning(disable:4786)

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

vector<string>
ExtractAndSortWords(const std::string& line)
{
    const string delims = " \t,";

    vector<string> words;

    string::size_type begIdx = line.find_first_not_of(delims);
    while (begIdx != string::npos)
    {
        string::size_type endIdx = line.find_first_of(delims, begIdx);

        words.push_back(line.substr(begIdx, endIdx-begIdx));

        begIdx = line.find_first_not_of(delims, endIdx);
    }

    std::sort(words.begin(), words.end());  
       
    return words;
}


int main(int argc, char* argv[])
{
    string line = "we, thank, you";

    vector<string> sortedWords = ExtractAndSortWords(line);

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

    return 0;
}

0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2701038
This doesn't generate the sample output that redgreenred asked for.  (In fact, I'm not sure exactly what redgreenred is asking for.)

..B ekiM
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:redgreenred
ID: 2701103
What I am asking is to break a string into different combinations of its substrings like

string: aa, bb, cc,

I want my output as

aa, bb, cc,
bb, cc, aa,
cc, aa, bb,


I havent tried the above code, if it is working for the fixed string then I just have to alter it to read from the text file. But let me try it first.

thanks
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2701716
OK, I missed part of what you were saying, your first message said that the words should be sorted, the code I gave sorts the words.

If you want all possible combinations of the set of words, the basic structure of what I gave is OK, since it extracts the words from the text, but you will need to add some code to generate the other combinations.

Obviously there are going to be an awful lot of combinations for anything but a small number of words.
0
 

Author Comment

by:redgreenred
ID: 2701896
the code is only breaking a string into three parts and then doing the sorting.

My requirement was to convert the string like this


we, thank, you
thank, you, we
you, we, thank

and then do sorting on the new strings.

thanks
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2703168
its still not clear what you want.  what do you mean by 'and then do sorting on the new strings'.

You just seem to have combinations of the words.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2703243
ok, I think I understand finally, replace the main program above with the following:

int main(int argc, char* argv[])
{
    string line = "we, thank, you";

    vector<string> sortedWords = ExtractAndSortWords(line);

    vector<string> permutations;

    while (next_permutation(sortedWords.begin(), sortedWords.end()))
    {
        std::string perm;
        for (size_t i=0; i<sortedWords.size(); i++)
        {
            perm += sortedWords[i];
            if (i < sortedWords.size()-1) perm += ", ";
        }
        permutations.push_back(perm);
    }

    std::sort(permutations.begin(), permutations.end());
    for (size_t i=0; i<permutations.size(); i++)
    {
        cout << permutations[i] << endl;
    }
    return 0;
}
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 2703265
sorry, I noticed that the previous code misses the first permutation.  Here is the corrected code:

int main(int argc, char* argv[])
{
    string line = "we, thank, you";

    vector<string> sortedWords = ExtractAndSortWords(line);

    vector<string> permutations;

    bool morePermutations = (sortedWords.size() > 0);
    while (morePermutations)
    {
        std::string perm;
        for (size_t i=0; i<sortedWords.size(); i++)
        {
            perm += sortedWords[i];
            if (i < sortedWords.size()-1) perm += ", ";
        }
        permutations.push_back(perm);
        morePermutations = next_permutation(sortedWords.begin(), sortedWords.end());
    }

    std::sort(permutations.begin(), permutations.end());
    for (size_t i=0; i<permutations.size(); i++)
    {
        cout << permutations[i] << endl;
    }
    return 0;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question