Apr 1, 2012

C Program to find all the permutations of a given string.

Write a c program to find all the permutations of a given string.
Example: If the given string is sam, output is,
sam
sma
msa
mas
asm
ams.
Read more about C Programming Language .

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

# include<stdio.h>
# include<conio.h>
#include<string.h>
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void permute(char *a, int i, int n)
{
    int j;
    if (i == n)
    printf("%s\n", a);
    else
    {
        for (j = i; j <= n; j++)
        {
            swap((a+i), (a+j));
            permute(a, i+1, n);
            swap((a+i), (a+j));
        }
    }
}


int main()
{
    char str[80] ;
    int len=0,i;
    puts("Enter a string:\n\n");
    gets(str);
    for (i=0; str[i] != '\0'; i++)
    {
        len++;
    }
    permute(str, 0, len);
    getchar();
    return 0;
}
Read more Similar C Programs
Searching in C

C Strings
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link