How to cin>> multiple strings and use them in functions

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.

#include <stdio.h>
#include <string.h>

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 n = strlen(str);
permute(str, 0, n-1);
return 0;
}
you could use a while loop to keep getting strings from a user

1
2
3
4
5
6
7
8
9
10
11
12

  while(cin >> example_string){

            // do something with the string


    }




When I tried that it gave me the error "cannot convert string to char * argument"

1
2
3
4
5
6
7
8
9
int main()
{
string str;
int i=0;
while(cin >> str){
	i++;
	permute(str, 0, i-1);};
return 0;
}
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));
       }
   }
}
first you need to include <cstring> or else it will fail to compile

I will try to get the code working with the c style string first

my bad I shouldn't have used a string I forgot you were using a c style string
Last edited on
Sorry about that this code now should work

if you have any questions about it feel free to ask I'll try to help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>

using namespace 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

sorry I couldn't be of any further help
Last edited on
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
large cats invade Q
Your possible headlines are:
large cats invade large invade cats cats large invade cats invade large
...
Enter a list of words
Topic archived. No new replies allowed.