2012-02-07 23 views
0

我必须迭代所有字母数字字符,包括小写字母和大写字母以及数字,每个字符都必须调用crypt命令。因此,我必须检查Aa,aA,1a,a1,1A,A1等,直到所有可能的组合并调用crypt crypt_file_file。如何遍历字母数字字符以获得2个字符的字符串

有没有一种有效的方法来做到这一点?

谢谢

+3

此网站为编程人员提供帮助。告诉我们你到目前为止所尝试过的。你也应该试着让你的问题更清楚,'我必须检查...'?检查什么?样本输入,预期输出,实际输出和代码使得人们可以提供帮助的问题变得更好。祝你好运。 – shellter 2012-02-07 22:59:00

+0

你将需要给予和举例,并更具体地说明你想要做什么。 – jkysam 2012-02-07 23:03:15

回答

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 
    } 
} 
+0

非常感谢你..这很有用:) – Shwethascar 2012-02-08 21:42:49

0

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

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; 
} 
0

所有可能的组合您正在使用62个字符:0-9,AZ,AZ 10 + 26 + 26 = 62。你想从所有可能的二字组合00至zz。把它们当作两位数字在基座62:

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

通过所有数字转到从0到3843(62^2 - 1)并将其转换为两位数字在基座62