/* a program to find all the permutations of an input string
pointers are not allowed*/

#include <stdio.h>
#include <string.h>
#include <conio.h>

void main()
{

    clrscr();
    int num =0;
    char buff[20] ;
    printf("Enter the string\n");
    scanf("%s",buff);


 int len = strlen(buff);
 printf("%s\n",buff);

 for(int i=0;i<len;i++)
 {
    for(int j=1;j<len;j++)
    {
        for(int k=j+1;k<len;k++)
        {
            char x = buff[j];
            char y = buff[k];
            if(x != y)
            {

                char tmp[10];
                for(int tem=0;tem<len;tem++)
                {
                    tmp[tem] = buff[tem];
                }

                tmp[j] = y;
                tmp[k] = x;
                tmp[len] = '\0';
                printf("%s\n",tmp);
            }
        }
    }
    if(i+1 < len)
    {
        char x = buff[0];
        char y = buff[i+1];

        if(x != y)
        {

            buff[0] = y;
            buff[i+1] = x;
            printf("%s\n",buff);

        }
    }
 }

 getch();

 }

/*this works for strings upto 3 characters.but more than 3,all permutns r not printed*/

Recommended Answers

All 3 Replies

>> char tmp[10];
tmp is too small -- increase to at least 20. Lets say I enter a string of 18 characters, the line tmp[len] = '\0'; will cause program crash.

[edit]Other than the above problem your program seems to work ok. I didn't check every line of the output, but the program spit out a whole bunch of lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.