# Thread: Creating a function that swaps two strings from within an array of strings

1. ## Creating a function that swaps two strings from within an array of strings

Hey everyone, I'm new to programming and I'm having difficulty with swapping strings now. I've to come up with a program that repeatedly prompts the user to enter a word until he types GO, which terminates the loop and causes the program to print out all possible permutations of the words entered. The execution trace should look like this:

Enter a word: many
Enter a word: dogs
Enter a word: jump
Enter a word: GO
many dogs jump
many jump dogs
....

I used recursion to solve this, and the idea for the recursion I'm using is from this link:

Write a program to print all permutations of a given string - GeeksforGeeks

Here is my code:

Code:
```#include <stdio.h>#include <string.h>

void permute ( char input[], int index, int end );

void swap ( char *x, char *y );

int main()
{
char input  = {""}, ch;
int n = 0, index = 0, a = 0;

printf("Enter a word: ");
while ( ( ch = getchar () ) != '\n' )
{
input [a] = ch;
a++;
}

while ( strcmp ( input [n] , "GO" ) != 0 )
{
a = 0;
n++;
printf("Enter a word: ");
while ( ( ch = getchar () ) != '\n' )
{
input [n][a] = ch;
a++;
}

}

permute ( input, index, n - 1 );

return 0;
}

void permute ( char input[], int index, int end )
{
if ( index == end )
{
printf("%s\n", input [index] );
return;
}

else
{
for ( int i = index; i <= end; i++ )
{
swap ( input [i], input [i+1]);
permute ( input, index + 1, end );
swap ( input [i], input [i+1] );

}
}
}

void swap ( char *x, char *y )
{
char temp  = {""};

strcpy ( temp, x );
strcpy ( x, y );
strcpy ( y, temp );

}```
Here is my execution trace:

Enter a word: manyEnter a word: words
Enter a word: jump
Enter a word: GO
many
GO
jump
GO
words
jump

------------------
(program exited with code: 0)

Press any key to continue . . .
Terminate batch job (Y/N)?
I don't know why it does not work, I've tried using gdb to debug using the backtrace function, but I get a No Stack message.

Can someone help me out here? I've been stuck for hours. Thanks! 2. I'm too lazy to describe the problems. Here's a rewrite. I couldn't conform to your "maximum number of spaces humanly possible" policy. If you find the following normal spacing hard to read, maybe you need glasses.
Code:
```#include <stdio.h>
#include <string.h>

#define MAXWORDS   100
#define MAXWORDLEN  20

void permute(char input[][MAXWORDLEN], int start, int end);
void swap(char *x, char *y);

int main() {
char words[MAXWORDS][MAXWORDLEN], input[MAXWORDLEN];
int n = 0;

while (1) {
int i, ch;
printf("Enter a word: ");
for (i = 0; i < MAXWORDLEN-1 && (ch = getchar()) != '\n'; i++)
input[i] = ch;
input[i] = '\0';
if (strcmp(input, "GO") == 0)
break;
if (n >= MAXWORDS)
break;
strcpy(words[n++], input);
}

permute(words, 0, n);

return 0;
}

void permute(char words[][MAXWORDLEN], int start, int end) {
if (start == end) {
for (int i = 0; i < end; i++)
printf("%s ", words[i]);
putchar('\n');
return;
}
for (int i = start; i < end; i++) {
swap(words[start], words[i]);
permute(words, start + 1, end);
swap(words[start], words[i]);
}
}

void swap(char *x, char *y) {
char temp[MAXWORDLEN];
strcpy(temp, x);
strcpy(x, y);
strcpy(y, temp);
}``` 3. Well the big problem would seem to be your use of <= in a loop, and indexing i+1 array elements. Popular pages Recent additions array 2 problem, input, int, recursion error, word 