Hi, I was doing some practice problems and one of them is to find all the different string permutations of ABC.
Here is my code which gives no errors or warnings but clearly my logic is wrong somewhere and I cannot find where.
Code:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void swap(char *a, char *b){
char temp;
temp = *a;
*a = *b;
*b = temp;
}
void permute( char *str, int i, int n){
int j, range = n - i;
if (i == n)
printf("%s\n", str);
else{
for( j = i; j <= range; j++){
swap(&str[i],&str[n]);
permute(str, i, j);
swap(&str[i],&str[n]);}
}
}
int main(void){
char *ptr;
char string[] = "ABC";
ptr = string;
permute(ptr,0,2);
return 0;}