You'd simply have to keep track of : has particular alphabet already been
used or not. You can do this by maintaining a 'used' array of 0/1 . Set and
unset the respective index before and after the recursion.
Here's the modified code:

#include<stdio.h>
#include<string.h>
#include<malloc.h>
*void allLexicographicRecur (char *str, char* data, int *used, int last,
int index)*
{
int i, len = strlen(str);
// One by one fix all characters at the given index and recur for the
// subsequent indexes
for ( i=0; i<len; i++ )
{
// Fix the ith character at index and if this is not the last index
// then recursively call for higher indexes
* if(used[i] == 0){*
data[index] = str[i] ;
* used[i] = 1;*

// If this is the last index then print the string stored in data[]
if (index == last)
printf("%s\n", data);
else // Recur for higher indexes
allLexicographicRecur (str, data, used, last, index+1);
* used[i] = 0;*
* }*
}
}

/* This function sorts input string, allocate memory for data (needed for
   allLexicographicRecur()) and calls allLexicographicRecur() for printing
all
   permutations */
// Needed for library function qsort()
int compare (const void * a, const void * b)
{
return ( *(char *)a - *(char *)b );
}
void allLexicographic(char *str)
{
int len = strlen (str) ;

// Create a temp array that will be used by allLexicographicRecur()
char *data = (char *) malloc (sizeof(char) * (len + 1)) ;
data[len] = '\0';

// Sort the input string so that we get all output strings in
// lexicographically sorted order
qsort(str, len, sizeof(char), compare);

// Now print all permutaions
* int used[len], i;*
* for(i=0; i<len; ++i)*
* used[i] = 0;*
*
*
* allLexicographicRecur (str, data, used, len-1, 0);*

// Free data to avoid memory leak
free(data);
}


// Driver program to test above functions
int main()
{
char str[] = "ABC";
printf("All permutations with repetition of %s are: \n", str);
allLexicographic(str);
getchar();
return 0;
}


On 30 October 2012 22:00, Nishant Pandey <[email protected]>wrote:

> void allLexicographicRecur (char *str, char* data, int last, int index)
> {
>     int i, len = strlen(str);
>
>     // One by one fix all characters at the given index and recur for the
>     // subsequent indexes
>     for ( i=0; i<len; i++ )
>     {
>         // Fix the ith character at index and if this is not the last
> index
>         // then recursively call for higher indexes
>         data[index] = str[i] ;
>
>         // If this is the last index then print the string stored in
> data[]
>         if (index == last)
>             printf("%s\n", data);
>         else // Recur for higher indexes
>             allLexicographicRecur (str, data, last, index+1);
>     }
> }
>
> /* This function sorts input string, allocate memory for data (needed for
>   allLexicographicRecur()) and calls allLexicographicRecur() for printing
> all
>   permutations */
> void allLexicographic(char *str)
> {
>     int len = strlen (str) ;
>
>     // Create a temp array that will be used by allLexicographicRecur()
>     char *data = (char *) malloc (sizeof(char) * (len + 1)) ;
>     data[len] = '\0';
>
>     // Sort the input string so that we get all output strings in
>     // lexicographically sorted order
>     qsort(str, len, sizeof(char), compare);
>
>     // Now print all permutaions
>     allLexicographicRecur (str, data, len-1, 0);
>
>     // Free data to avoid memory leak
>     free(data);
> }
>
> // Needed for library function qsort()
> int compare (const void * a, const void * b)
> {
>     return ( *(char *)a - *(char *)b );
> }
>
> // Driver program to test above functions
> int main()
> {
>     char str[] = "ABC";
>     printf("All permutations with repetition of %s are: \n", str);
>     allLexicographic(str);
>     getchar();
>     return 0;
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to