stef
stef

Reputation: 13

Program returns all permutations of n elements?infinite loop?

This code should output all permutations of n elements, So when i enter for example 3 Output it must :

ABC BAC CAB ACB BCA CBA

with this code :

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

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

    }
  }
}
int main(){
  int x , n ;
  int i = 0 ;
  char str[26];
  printf("prem ");
  scanf("%d",&x);
  while (x > 0){
    str[i] = 'A' + i ;
    i++;
    x--;
  }
  n = strlen(str);
  permute(str, 0, n-1);

}

I should type what I want for a number eg 3 for ABC or 4 for ABCD and then the code should handle that but it didnt workea and it goes in an infinite loop

but for this Code :

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

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

    }
  }
}
int main()
{
  char str[26];
  gets(str);
  int n = strlen(str);
  permute(str, 0, n-1);
  return 0;
}

I can enter whatever i want and it handle with it without Problem

my question is what is the problem with the first code? i mean i want to enter a number so that the program: if i type 3 for example it should deal with ABC

Upvotes: 1

Views: 225

Answers (3)

p-a-o-l-o
p-a-o-l-o

Reputation: 10047

Your main should be like:

int main(){
  int x , n ;
  int i = 0 ;
  char str[26];
  printf("prem ");
  scanf("%d",&x);

  //user entered the string length in x, let' save it in n
  n = x;

  while (x > 0){
    str[i] = 'A' + i ;
    i++;
    x--;
  }

  //n = strlen(str); ===> this is wrong, the user did not entered a string

  permute(str, 0, n-1);
}

In the working code (the second block of code you posted), the user enters a string which is stored in str variable:

gets(str);

Then the string length is passed to permute function using strlen:

int n = strlen(str);
permute(str, 0, n-1);

In the first (non working) block of code, you ask the user to enter a length, not a string, and store it in x. You then put x characters in str array, but this is not a proper c string, because a null terminator is missing. So strlen return value is pretty much unreliable.

If you don't like the solution I suggested, you can just provide a

str[i] = '\0';

right after your while block and leave everything else untouched, and it will work anyway.

Upvotes: 0

sg7
sg7

Reputation: 6298

The issue is that the str string is not properly terminated therefore result of n = strlen(str); is undefined. strlen counts characters till \0 is encountered.

int main(){
int x , n ;
int i = 0 ;
char str[26];
printf("prem ");
scanf("%d",&x);
while (x > 0){
    str[i] = 'A' + i ;
    i++;
    x--;
}

str[i] = 0; // terminate the string

n = strlen(str);
permute(str, 0, n-1);

}

Upvotes: 0

Yaniv Shaked
Yaniv Shaked

Reputation: 758

The problem is that strlen is invoked on a string which is not terminated.

Either add '\0' at the end of the string to terminal it:

while (x > 0){
    str[i] = 'A' + i ;
    i++;
    x--;
}
str[i] = '\0';

OR (better)

invoke permute with the original value of x (save it after the scanf).

Upvotes: 1

Related Questions