I'm trying to write a program that can output written words in every possible way.
e.g.
Input:
I am coding
Output:
I am coding
I coding am
am I coding
am coding I
coding I am
coding am I
here's my code:
#include <iostream>
using namespace std;
int main()
{
string data;
cout << "enter data: "; getline(cin, data);
// counting amount of the words //
int size = 1, word = 0;
for(int i=0; i<data.size(); i++)
{
if(data[i] == ' ') size++;
}
string msg[size];
// each index is one word //
for(int i=0; i<data.size(); i++)
{
if(data[i] == ' ') { word++; continue; }
msg[word] += data[i];
}
// output words in every possible way //
for(int i=0; i<size; i++)
{
for(int j=0; j<size; j++)
{
for(int k=0; k<size; k++)
{
if(i==j || i==k || j==k) continue;
cout << msg[i] << ' ' << msg[j] << ' ' << msg[k] << endl;
}
}
}
}
Is there a way to make it work on N nested loops for any other amount of words?
I also tried to do it using factorials but it didn't work.