Domanda anagrammi C++

Stato
Discussione chiusa ad ulteriori risposte.

Pr0Ryder93

Utente Electrum
14 Gennaio 2010
264
67
14
174
Codice:
#include <stdio.h>
#include <string.h>

//prototipi di funzione
void Swap(char* string,int i,int j);
void Permutations(char *string,int i ,int n);


int  main()
{
    char s[256];//poi per lungezze maggiori ti arrangi tu ma tanto il tempo di  esecuzione
                //cresce col fattoriale e non conviene esagerare con la lungezza....
    printf("Dammi la stringa (max 255 caratteri):");
    scanf("%s",s);
    Permutations(s,0,(int)strlen(s)-1);
}


//Genera ricorsivamente le permutazioni
void Permutations(char *string,int i ,int n)
{
    int j;
    if(i==n)
    {
        for(j=0;j<=n;j++)
            printf("%c",string[j]);
        printf("\n");
    }
    else
    {
        for( j = i ; j<=n ; j++ )
        {
            Swap(string,i,j);
            Permutations(string,i+1,n);
            Swap(string,i,j);
        }
    }
}

//Effettua un semplice scambio di posizione
void Swap(char* string,int i,int j)
{
    int temp;
    temp = string[i];
    string[i] = string[j];
    string[j] = temp;
}


--- Aggiornamento Post ---

DA DIRE... non e mio il programma l ho preso da un sito e la prima volta mi scuso xD
 
Stato
Discussione chiusa ad ulteriori risposte.