如何为包含2个字符的字符串迭代字母数字字符

时间:2012-02-07 22:55:33

标签: c unix encryption

我必须遍历所有字母数字字符,包括小写和大写字母以及数字,对于每一个字符,我必须调用crypt命令。

所以,我必须检查Aa,aA,1a,a1,1A,A1等,直到所有可能的组合并调用crypt KEY crypt_file。

有一种有效的方法吗?

由于

4 个答案:

答案 0 :(得分:0)

您可以将字符放入数组中。类似的东西:

const char vals[] = { '0', '1', '2', ..., 'a', 'b', ... 'Z' }; // Probably better to generate it programatically

双“for”循环应该这样做:

int i;
for (i = 0; i < kNumVals; i++) // kNumVals is the number of characters in your array
{
    int j;
    for (j = 0; j < kNumVals; j++)
    {
        char myString[3] = {vals[i],vals[j],'\0'};
        // Do something with myString
    }
}

答案 1 :(得分:0)

最有效的方法是生成所有组合并将其存储在文件中。然后只需读取文件,因为组合不会改变。

答案 2 :(得分:0)

输入str中的字符列表,即“aA1”。这是为了检查所有可能的组合,如aA1,1Aa等。

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


void swap(char*,int);
void gotoloop(char*,int);

void main()
{
char *ch;
int i,j,k,l;
ch=(char*)malloc(20);
//clrscr();
printf("Enter the string\n"); //Enter AAaa11 for your case
gets(ch);

l=strlen(ch);
gotoloop(ch,l);
//if flag !=false call encryption.
return;
}

void gotoloop(char *ch,int l)
{
int i,k;
k=l;

if(l<=1) 
return;


for(i=0;i<k;i++)
{
swap(ch,k);
l--;
gotoloop(ch,l);
l++;
if(k==2) {
 printf("\n%s ",ch);//check this from the list, if not found set FLAG=false
}
}

}


void swap(char *ch,int r)

{
char c;
int i;

c=ch[r-1];
for(i=r-1;i>0;i--)
ch[i]=ch[i-1];
ch[0]=c;
}

答案 3 :(得分:0)

您使用的是62个字符:0-9,A-Z,a-z 10 + 26 + 26 = 62.您需要从00到zz的所有可能的两个字符组合。将它们视为基数62中的两位数字:

 0 -> 00
 1 -> 01
10 -> 0A
61 -> 0z
62 -> 10

浏览0到3843(62 ^ 2 - 1)之间的所有数字,并将它们转换为基数62中的两位数字。