redgreenred
asked on
sorting substrings
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.
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.
Again homework.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
..B ekiM
ASKER
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
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
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.
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.
ASKER
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
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
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.
You just seem to have combinations of the words.
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(sortedWo rds.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(per m);
}
std::sort(permutations.beg in(), permutations.end());
for (size_t i=0; i<permutations.size(); i++)
{
cout << permutations[i] << endl;
}
return 0;
}
int main(int argc, char* argv[])
{
string line = "we, thank, you";
vector<string> sortedWords = ExtractAndSortWords(line);
vector<string> permutations;
while (next_permutation(sortedWo
{
std::string perm;
for (size_t i=0; i<sortedWords.size(); i++)
{
perm += sortedWords[i];
if (i < sortedWords.size()-1) perm += ", ";
}
permutations.push_back(per
}
std::sort(permutations.beg
for (size_t i=0; i<permutations.size(); i++)
{
cout << permutations[i] << endl;
}
return 0;
}
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(per m);
morePermutations = next_permutation(sortedWor ds.begin() , sortedWords.end());
}
std::sort(permutations.beg in(), permutations.end());
for (size_t i=0; i<permutations.size(); i++)
{
cout << permutations[i] << endl;
}
return 0;
}
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(per
morePermutations = next_permutation(sortedWor
}
std::sort(permutations.beg
for (size_t i=0; i<permutations.size(); i++)
{
cout << permutations[i] << endl;
}
return 0;
}