string decoder
May 2, 2010 at 1:08pm May 2, 2010 at 1:08pm UTC
hey all!
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:
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
#include <cstdlib>
#include <iostream>
#include <string>
using namespace 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!
May 3, 2010 at 6:40am May 3, 2010 at 6:40am UTC
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
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string a = "Hi!" ; // Can't be bothered with input from user.
bool found = false ;
int num = myString.length()+1; // An extra char for NULL-terminating.
char * b = new char [num];
for (int x = 0; x < num; ++x)
copy[x] = 0; // Init to zero.
for (int i = 0; (i <= 300) && !found; ++i) { // Three letters. Three levels of loops.
for (int j = 0; (j <= 300) && !found; ++j) {
for (int k = 0; (k <= 300) && !found; ++k) {
copy[0] = i;
copy[1] = j;
copy[2] = k;
if (strcmp(copy, myString.c_str()) == 0)
// Match found.
found = true ;
}
}
}
if (found)
cout << "String is: " << b << endl;
else
cout << "Couldn't copy string." << endl;
system("PAUSE" );
return EXIT_SUCCESS;
}
May 3, 2010 at 7:00am May 3, 2010 at 7:00am UTC
Doh! I like to use different variable names to you.
With this algorithm, anything more than 4 or 5 letters becomes a little too long to wait for. Reducing the scope of ASCII table values to check helped a little, but nothing to brag about.
I don't know how you'd modify this code to work for any given length string.
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
#include<iostream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string a = "Hi!" ; // Can't be bothered with input from user.
bool found = false ;
int num = a.length()+1; // An extra char for NULL-terminating.
char * b= new char [num];
for (int x = 0; x < num; ++x)
b[x] = 0; // Init to zero.
for (int i = 32; (i <= 126) && !found; ++i) { // Three letters. Three levels of loops.
for (int j = 32; (j <= 126) && !found; ++j) {
for (int k = 32; (k <= 126) && !found; ++k) {
b[0] = i;
b[1] = j;
b[2] = k;
if (strcmp(b, a.c_str()) == 0)
// Match found.
found = true ;
}
}
}
if (found)
cout << "String is: " << b << endl;
else
cout << "Couldn't copy string." << endl;
system("PAUSE" );
return EXIT_SUCCESS;
}
Topic archived. No new replies allowed.