打印单词的所有变体

时间:2013-05-07 07:09:33

标签: c passwords generator

所以这是我想要制作的节目: 创建密码生成器,它使用字典和匹配文件通过将字典单词中的字母替换为匹配来生成密码。 例: 字典文件: 苹果 环 匹配文件: 一个4 e 3 o 0 输出: 4pple appl3 4ppl3 l0op lo0p l00p

这是我的解决方案:

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

/* function prototypes */
int read_source(char* ,char* ,char* );
char* shift_string(char* ,char* );
int write_shifted(char* ,char* );

int main(int argc, char *argv[])
{
/* argv[1]: the source file with the original strings
 * argv[2]: the shift file which indicates which characters are to be
 * replaced with what
 * argv[3]: the file in which the new strings are to be stored*/

if(!argv[1])
{
    printf("No source file, can do nothing. Exiting.\n");
    return 1;
}
else if (!argv[2])
{
    printf("No shift file given, can do nothing. Exiting.\n");
    return 2;
}
else if (!argv[3])
{
    /* If no output file has been specified, print the
     * output directly to the console. */
    read_source(argv[1],argv[2],NULL);
    return 0;
}

if (read_source(argv[1],argv[2],argv[3])!=0)
{
    printf("There has been an error. Exiting.\n");
    return 3;
}

if (argv[3])
{
    printf("Everything seems to have gone according to plan.\n");
    printf("Your output has been stored in \"%s\"\n",argv[3]);
}

return 0;
}

int read_source(char* source_file,char* shift_file,char* out_file)
{
FILE *file_pointer;
file_pointer=fopen(source_file, "r");

/* Exit gracefully if source_file cannot be found. */
if (file_pointer == NULL) 
{
     printf("Couldn't open \"%s\" for reading.\n",source_file);
     return 1;
}

char* new_line;

    /* maximum line length, can be changed if needed */
char line[256];

/* Go through the source file. */
while (fgets(line,sizeof line,file_pointer) != NULL)
{
    new_line = shift_string(shift_file,line);
    if (new_line==NULL)
    {
        printf("There has been an error with replacing the characters.\n");
        return 2;
    }
    write_shifted(new_line,out_file);
}
int fclose(FILE *file_pointer);

return 0;
 }


 char* shift_string(char* shift_file,char* source_string)
 {
/* This function replaces certain characters in a given source_string as
 * specified by shift_file */


/* Open shift file. */
FILE *file_pointer;
char i,j;
file_pointer=fopen(shift_file, "r");

/* Exit gracefully if shift_file cannot be found. */
if (file_pointer == NULL) 
{
     printf("Couldn't open \"%s\" for reading.\n",shift_file);
     return NULL;
}

int k;

/* Determine how long the source_string is for the loop below. */
int length = strlen(source_string);

while (fscanf(file_pointer, "%c %c\n", &i, &j)==2)
{

    /* This loop actually does the replacing. */
    for (k=0;k<length;k++)
    {
        if (source_string[k]==i) 
        {
            source_string[k]=j;
        }
    }
}
int fclose(FILE *file_pointer);

return source_string;
}

int write_shifted(char* new_line,char* out_file)
{
/* This function writes the new strings to out_file
 * If no out_file has been given, it will write the
 * output to the console.*/

if (out_file==NULL)
{
    printf("%s",new_line);
    return 0;
}

FILE *file_pointer; 

/* Open in a+ mode. If out_file does not yet exist, it will be created.
 * If it does exist, it will be appended to instead of overwritten. */
file_pointer = fopen(out_file,"a+"); 

/* Write the new line */
fprintf(file_pointer,"%s",new_line);

fclose(file_pointer);
return 0; 
} 

所以问题是现在它只打印最终状态(4ppl3 l00p)但我也需要中间状态(4pple appl3 l0op lo0p)。有人能给我一些线索如何制作它。提前致谢。 :)

1 个答案:

答案 0 :(得分:0)

递归适用于此类问题。基本上,取决于是否用第一个字母替换,并用每个案例的其余部分递归 - 替换而不是替换。这样做,直到你没有任何字母,你将拥有所有的组合。

// terribly pseudo-codey, but should give the idea
string='abc';
stringfunction("", string);

// simplified, just shifts each letter by one
stringfunction(processed, unprocessed)
  if unprocessed is empty
    print processed
    return

  stringfunction(strcat(processed, unprocessed[0]), unprocessed[1]);
  stringcunction(strcat(processed, unprocessed[0]+1), unprocessed[1]);

这将打印出abc,abd,acc,acd,bbc,bbd,bcc,bcd

当然,对于你的情况,你要么做或不做替换。