Currently, I am working on the exercises from w3resource about pointers. Help me explaining the parameters of charPermu and why it is given (str, 0, n -1) in the main code please?
#include <stdio.h>
#include <string.h>
#define MAX 4
void changePos(char *ch1, char *ch2){
char tmp;
tmp = *ch1;
*ch1 = *ch2;
*ch2 = tmp;
}
void charPermu(char *cht, int stno, int endno){
int i;
if(stno == endno){
printf("%s ", cht);
} else{
for(i = stno; i<= endno; i++){
changePos((cht+stno), (cht+i));
charPermu(cht, stno+1, endno);
changePos((cht+stno), (cht+i));
}
}
}
int main(){
char str[MAX];
printf("\n\n Pointer : Generate permutations of a given string :\n");
printf("--------------------------------------------------------\n");
printf("Enter a string to permutate: ");
scanf("%[^\n]", str);
int n = strlen(str);
printf("The permutations of the string are: ");
charPermu(str, 0, n - 1);
printf("\n\n");
return 0;
}
Wow, for a tutorial, that is terrible variable naming. Let's give things more useful names:
#include <stdio.h>
#include <string.h>
#define MAX_STRING_LENGTH 4
void swapCharacters(char *ch1, char *ch2){
char tmp;
tmp = *ch1;
*ch1 = *ch2;
*ch2 = tmp;
}
void printPermutations(char *fullString, int startingOffset, int endingOffset){
int nextOffset;
if(startingOffset == endingOffset){
printf("%s ", fullString);
} else{
for(nextOffset = startingOffset; nextOffset<= endingOffset; nextOffset++){
swapCharacters((fullString+startingOffset), (fullString+nextOffset));
printPermutations(fullString, startingOffset+1, endingOffset);
swapCharacters((fullString+startingOffset), (fullString+nextOffset));
}
}
}
int main(){
char input[MAX_STRING_LENGTH];
printf("\n\n Pointer : Generate permutations of a given string :\n");
printf("--------------------------------------------------------\n");
printf("Enter a string to permutate: ");
scanf("%[^\n]", input);
int stringLength = strlen(input);
printf("The permutations of the string are: ");
printPermutations(input, 0, stringLength - 1);
printf("\n\n");
return 0;
}
These names probably aren't perfect, but at least they read as English. Always remember that variable names are for the aid of the human reader, not the computer.
Now we can look at the parameters, and see that:
The idea is that each execution of the function swaps one character, and then calls itself to generate all the permutations of the rest of the string after that point.
So when startingOffset is zero, the for loop is putting each character of the string in turn in first place, then "fixing" that first character for the recursive call; that will then loop over each possible character for second place (offset 1), and fix that, and so on. Once all characters are "fixed", starting and ending offsets will be the same, and we print the result and return to carry on with a different combination.