I'm trying to write code that takes in a list of words from the user until the letter "Q" is entered and then prints out all the possible word combinations. My first problem is I don't know how to cin>> multiple strings when I'm not sure how many there will be. I also have example code for a recursive algorithm that does the permutations but I'm not sure how to use strings or char arrays with the code.
I'm not sure how to change this code to work with strings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void permute(char *a, int start, int end)
{
int i;
if (start == end)
cout << a << endl;
else
{
for (i = start; i <= end; i++)
{
swap((a+start), (a+i));
permute(a, start+1, end);
swap((a+start), (a+i));
}
}
}
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
usingnamespace std;
void swap(char *x, char *y) {
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int start, int end) {
int i;
if (start == end)
printf("%s\n", a);
else {
for (i = start; i <= end; i++) {
swap((a + start), (a + i));
permute(a, start + 1, end);
swap((a + start), (a + i));
}
}
}
int main() {
char str[] = "ABC";
int one;
while(cin >> str){
int n = strlen(str);
permute(str, 0, n - 1);
return 0;
}
}
that fixed the problem as to the input but as to why your program only prints out the first four letters that I can't explain but if anybody else see's this I'm sure they will be glad to help
So this is definitely closer but what I ultimately want it to do is print out all possible combinations of words entered. It should input words until "Q" is entered. I'm just not sure on the syntax for the permutation function to find the permutations of string combination rather than the permutations of the strings themselves.
EX.
Enter a list of words largecatsinvadeQ
Your possible headlines are: large cats invadelarge invade catscats large invadecats invade large
...
Enter a list of words