0

I have written a program which prints all the permutations of a given string. But it was printing some weird things. The code goes as follows:

#include <stdio.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("%d\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(void)
{
  char a[100];
  gets(a);

  int k;
  k=strlen(a);
  permute(a, 0, k-1);

  system("pause");
}

It was printing some numbers instead of given string.. plz help

2
  • 2
    Please fix the indentation, this is way less readable than it should be. Commented Oct 9, 2012 at 12:59
  • This isn't your immediate problem, but, never ever use gets. Terrible things will happen if someone types more than 100 characters. Commented Oct 9, 2012 at 13:03

1 Answer 1

8

There is your problem:

 printf("%d\n", a);

should be

 printf("%s\n", a);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.