Wrong Data Output When Generating Word List
I needed code to generate a word list from a pre-specified character set and starting at a specific position so i can stop then continue later. My code isn't that good, so i would really appreciate any help fixing it or hints on making it faster/more efficient or any comments.
Here is the current output:
Data = aaa
Data = aab
Data = aac
Data = aba
Data = abb
Data = abc
Data = aca
Data = acb
Data = acc
Data = a
Data = a
Data = a
Data = baa
Data = bab
Data = bac
Data = bba
Data = bbb
Data = bbc
Data = bca
Data = bcb
Data = bcc
Data = b
Data = b
Data = b
Data = caa
Data = cab
Data = cac
Data = cba
Data = cbb
Data = cbc
Data = cca
Data = ccb
Data = ccc
Data = c
Data = c
Data = c
the output should be (i guess)
Data = aaa
Data = aab
Data = aac
Data = aba
Data = abb
Data = abc
Data = aca
Data = acb
Data = acc
Data = baa
Data = bab
Data = bac
Data = bba
Data = bbb
Data = bbc
Data = bca
Data = bcb
Data = bcc
Data = caa
Data = cab
Data = cac
Data = cba
Data = cbb
Data = cbc
Data = cca
Data = ccb
Data = ccc
which are all the possible combinations of the 3 letters character set of 3 letters combination
and here is my c code
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <stdbool.h>
char *charset = "abc";
int len = 3;
char *str;
int CharPos(char c)
{
int i;
for (i = 0; i < len; i++)
{
if (charset[i] == c) break;
}
if (i == strlen(charse开发者_Go百科t)) i = 0;
return i;
}
void generate(int pos)
{
while(str[0] != 0)
{
bool zero = false;
int y = 0;
while (str[len - 1] != 0)
{
printf("Data = %s\n", str);
str[len - 1] = charset[++y];
if (zero)
{
zero = false;
break;
}
Sleep(100);
}
int x = len;
while (x)
{
x--; // x = 1
if (str[x] != 0)
{
int charpos = CharPos(str[x]); // str[x] = a, charpos = 0
str[x] = charset[++charpos]; //aba
if (str[x] == 0) zero = true;
break;
}
else
{
str[x] = charset[0];
}
}
str[len - 1] = charset[0];
}
return;
}
int main()
{
str = malloc(len);
strcpy(str, "aaa");
generate(len - 1);
return 0;
}
You can use this:
#include <stdio.h>
#include <string.h>
int main(void){
char str[] = "aaa";
int len = strlen(str);
int idx = 0;
while(idx < len){
while(str[idx] <= 'c') {
printf("%s\n",str);
str[idx]++;
}
str[idx] = 'a';
while(++idx<len) {
str[idx]++;
if(str[idx] <= 'c') {
idx = 0;
break;
}
str[idx] = 'a';
}
}
return 0;
}
So what you want is all possible combinations of a given string.
So I took a look at it, wrote down the sequence you wanted in terms of indexes in the given string:
"abc"
aaa = 000
aab = 001
aac = 002
aba = 010
abb = 011
abc = 012
aca = 020
acb = 021
acc = 022
baa = 100
... and so forth.
This is basically counting from 0 up to some number in some base. The number of possible combinations here is 3^3, so you are basically counting up to 27 in base 3. More generally: Let x be the length of the given string. We are then counting from 0 up to x^x in base x.
You can then use the number representation in base x to find the n-th combination.
I wrote a quick solution in java:
static void combinations(String str) {
int base = str.length();
int combinations = (int) Math.pow(base, base);
for (int i = 0; i < combinations; i++) {
int c = i;
char[] arr = new char[base];
for (int j = arr.length-1; j >= 0; j--) {
arr[j] = str.charAt(c % base);
c /= base;
}
System.out.println(Arrays.toString(arr));
}
}
combinations("abc") gives me:
aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
...
Here's a possible solution in C(first time using C):
#include <stdio.h>
#include <string.h>
#include <math.h>
int
main() {
char *str = "abc";
int len = strlen(str);
int combinations = len;
int j = 1;
while (j++ < len) {
combinations = combinations*len;
}
int i;
for(i = 0; i < combinations;i++) {
char arr[len];
int c = i;
int j;
for (j = len-1; j >= 0;j--) {
arr[j] = str[c % len];
c /= len;
}
printf("%s\n", arr);
}
return 0;
}
精彩评论