-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
120 lines (105 loc) · 2.67 KB
/
main.c
File metadata and controls
120 lines (105 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdbool.h>
#include <unistd.h> // cross-platform
#include <math.h> // please use "gcc main.c -o main -lm" to compile in Linux
#define CHAR word[pos]
#define SPECIAL_CHAR ch == 'A' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 's' || ch == 'G'
#define SPECIAL_NUM ch == '4' || ch == '3' || ch == '1' || ch == '0' || ch == '5' || ch == '6'
int special_chars_quantity(char*);
void switch_char(int,char*);
void switch_right_side_back_to_letters(int,char*);
bool is_special_letter(char);
bool is_special_number(char);
void make_the_swap(int,char*);
int generate_alphanum_wordlst(char*,char*);
int main(int argc, char *argv[])
{
// main.c input.txt output.txt # 3 arguments
// read file exists
if (argc == 3 && access(argv[1], F_OK ) != -1) {
generate_alphanum_wordlst(argv[1],argv[2]);
}
return 0;
}
int generate_alphanum_wordlst(char *dicionary, char *wordlist) // switch_char all the possible combinations
{
int n_special_chars, pos, i;
char word[40];
FILE *wd = fopen(wordlist,"a");
FILE *dc = fopen(dicionary,"r");
while (fgetc(dc) != EOF) {
fscanf(dc,"%s",word);
pos = strlen(word)-1;
n_special_chars = special_chars_quantity(word);
fprintf(wd,"%s\n",word);
for (i=1; i<pow(2,n_special_chars); i++) {
switch_char(pos,word);
fprintf(wd,"%s\n",word);
}
}
return 0;
}
int special_chars_quantity(char *word) // count number of characters that can be swaped
{
int n = 0;
for (int pos = 0; pos < (int) strlen(word); pos++) {
if (is_special_letter(CHAR)) n++;
}
return n;
}
void switch_char(int pos, char *word)
{
if (is_special_letter(CHAR)) {
make_the_swap(pos,word);
pos++;
switch_right_side_back_to_letters(pos,word);
} else {
pos--;
switch_char(pos,word);
}
}
void switch_right_side_back_to_letters(int pos, char *word) // swap numbers to letters from 'pos' on the right side
{
while (pos < (int) strlen(word)) {
if (is_special_number(CHAR)) {
make_the_swap(pos,word);
}
pos++;
}
}
bool is_special_letter(char ch)
{
if (SPECIAL_CHAR) {
return true;
} else {
return false;
}
}
bool is_special_number(char ch)
{
if (SPECIAL_NUM) {
return true;
} else {
return false;
}
}
void make_the_swap(int pos, char *word) // num to letter and letter to char
{
switch (CHAR) {
case 'A': CHAR = '4'; break;
case 'o': CHAR = '0'; break;
case 'e': CHAR = '3'; break;
case 'i': CHAR = '1'; break;
case 'G': CHAR = '6'; break;
case 's': CHAR = '5'; break;
case '4': CHAR = 'a'; break;
case '0': CHAR = 'o'; break;
case '3': CHAR = 'e'; break;
case '1': CHAR = 'i'; break;
case '5': CHAR = 's'; break;
case '6': CHAR = 'G'; break;
default: break;
}
}