I am writing a program that takes a string, and tries to create the copy of it using all possible combinations, like a sort of bruteforce.
Can someone help me? this is what i have:
#include <cstdlib>
#include <iostream>
#include <string>
usingnamespace std;
int main(int argc, char *argv[])
{
string a;
getline(cin,a);
char b[a.length()];
for(int l = 0;l<a.length();l++){
b[l]='a';
}
int num = 0;
for(int i = 0;i<a.length();i++){
for(int z = b[i];z<300;z++){
b[i]=z;
cout<<b<<endl;
}
if(a==b){
cout<<"works";
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
This doesnt work. I am trying to decode using the ansi table, witch every character of the array b starts at a, and add to it until it is 300.
Can someone point out what i am doing wrong?
Thanks in advance!
no this is not what i want.
I want the program to pick a string, lets say bbbb.
the program should go like this:
aaaa
aaab
aaac...
.....
azzz
baaa
baab
baac...
...
bbba
bbbb
and then print out the number of guesses it took to get the string
And if you can, could you explain how to write the same code again, but with different checking without knowing the length of the word, for example
a
b
...
z
aa
ab
...
zz
aaa
aab
...
zzz
and so on
I also added a wider range of characters to it like this:
I hope I did well explaining this, and please excuse my english.
1 2 3 4 5 6 7 8 9 10 11 12
if( tmp[pos]<'z' ) { //1. if the last char is a-y then increament by 1
tmp[pos]++;
std::cout << tmp << "\n"; //2. then print
if( pos<tmp.length()-1 ) pos = tmp.length()-1; //5. alrighty the position
//at the back is already
//increamented, let's go home
//it is now aab[a]
}
elseif( tmp[pos]=='z' ) { //3. we can't increament z
tmp[pos] = 'a'; // so go back to a
pos--; //4. move back, for example aaa[z] moves to aa[a]a
}