Header-C
Header-C

 

Objective

The objective of this program is to generate and display all possible permutations of a given string. Permutations are arrangements of the characters in the string, and this program will help in understanding recursion and backtracking in programming.

C Program Code


#include 
#include 

// Function to swap characters at position i and j
void swap(char *x, char *y) {
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

// Recursive function to generate permutations
void permute(char *str, int left, int right) {
    if (left == right) {
        printf("%s\n", str);
    } else {
        for (int i = left; i <= right; i++) {
            swap((str + left), (str + i)); // Swap for the next permutation
            permute(str, left + 1, right);  // Recur
            swap((str + left), (str + i));  // Backtrack to the original string
        }
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);
    int n = strlen(str);
    printf("All permutations of the string are:\n");
    permute(str, 0, n - 1);
    return 0;
}

Program Structure

The program consists of the following main components:

  • Swap Function: This function swaps two characters in the string, which is essential for generating permutations.
  • Permute Function: This recursive function generates permutations by swapping characters. It calls itself to handle the next position in the string, and backtracks after each permutation is printed.
  • Main Function: This is the entry point of the program where the user inputs a string, and the permutation process is initiated.

How to Run the Program

  1. Copy the provided C code into a text editor and save it with a .c extension, for example, permutations.c.
  2. Open a terminal or command prompt.
  3. Navigate to the directory where the file is saved.
  4. Compile the program using a C compiler (e.g., gcc permutations.c -o permutations).
  5. Run the compiled program using ./permutations (on Unix/Linux) or permutations.exe (on Windows).
  6. Enter a string when prompted, and the program will display all permutations of that string.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)