c - 打印单词的所有变体

标签 c passwords generator

所以这是我正在尝试制作的程序: 创建密码生成器,它使用字典和匹配文件通过将字典单词中的字母替换为匹配项来生成密码。 例子: 词典文件: 苹果 环形 匹配文件: 一个 4 e 3 Ø 0 输出: 4苹果 应用程序3 4人3人 10操作 洛普 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)。有人能给我一些如何制作的线索吗?提前致谢。 :)

最佳答案

递归很适合解决此类问题。基本上,考虑是否替换第一个字母,并针对每种情况递归单词的其余部分 - 替换和未替换。这样做,直到你没有剩下任何字母,你就会得到所有的组合。

// 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

当然,对于您的情况,您可以选择进行或不进行替换,而不是移动字母。

关于c - 打印单词的所有变体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413204/

相关文章:

java - 从 Java 过渡到 C 代码组织

c - 尽管在这两种情况下,编译器在编译期间都不知道 n ,但编译器的工作在这两种情况下有何不同?

python - 协程、延续、生成器

javascript - 控制.each循环nodejs内的执行流程

c++ - 比较有符号比无符号整数更快

有人可以帮助如何在多个函数中声明 int 吗? C

bash - 检查是否 SSH 提示输入密码

php - 如何从数据库和 auth 用户中收回加盐密码?

email - Laravel 5.1 重置密码功能;用户的电子邮件在不同的表中

iterator - 从压缩迭代生成间隙元组的生成器