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

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    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[][20], int index, int end );
    
    
    void swap ( char *x, char *y );
    
    
    int main() 
    {
        char input [100][20] = {""}, ch;
        int n = 0, index = 0, a = 0;
    
    
        printf("Enter a word: ");
        while ( ( ch = getchar () ) != '\n' )
        {
            input [0][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[][20], 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 [20] = {""};
    
    
        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. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the big problem would seem to be your use of <= in a loop, and indexing i+1 array elements.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating array of strings in C
    By nerio in forum C Programming
    Replies: 4
    Last Post: 12-18-2015, 12:28 AM
  2. Creating an array of Strings
    By Vegenad in forum C Programming
    Replies: 5
    Last Post: 03-19-2010, 06:05 AM
  3. creating an array of strings
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2004, 07:40 AM

Tags for this Thread