0% found this document useful (0 votes)
2 views43 pages

C Programming

The document contains a C program that generates and prints all permutations of a given string. It includes a swap function to interchange characters and a permute function that recursively generates permutations. The main function prompts the user for input and initiates the permutation process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views43 pages

C Programming

The document contains a C program that generates and prints all permutations of a given string. It includes a swap function to interchange characters and a permute function that recursively generates permutations. The main function prompts the user for input and initiates the permutation process.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Programming

DATE-FEB26
#include<stdio.h>
#include <string.h>
// Swap using index (no pointers)
void swap(char str[], int i, int j)
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
// Function to generate permutations
void permute(char str[], int left, int right)
{
if(left == right)
{
printf("%s\n", str);
return;
}
for(int i = left; i <= right; i++)
{
swap(str, left, i); // swap
permute(str, left+1, right);
swap(str, left, i); // backtrack
}
}
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int n = strlen(str);
permute(str, 0, n-1);
return 0;
}

You might also like