This Forum References:

Programming in C++

Programming in C++

The time has come. Your destiny is taking shape. You are taking your next big leap in your journey of becoming the greatest software developer the world has ever seen! Well maybe... Don't hold us liable for that or anything...

String


5 replies [Last post]
WiBit
WiBit's picture
Offline
When things break I did it. I am an admin!I don't know when to shut up.
Joined: 2011-04-13
Points: 50

Questions or comments for String.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cbrad
cbrad's picture
Offline
...burning like a watchful eye...I am a moderatorI've been here since the first tree. I am betaMy name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!I don't know when to shut up.We propose a toast! To you!I found a bug so fix it!
Joined: 2011-06-14
Points: 2785

My little project that couldn't ...

I've taken it upon myself to play with some loops and thought to see how long would it take for a good computer to guess my password using brute force.  It seems harder than I first thought.

if I know the length of the password (let's say 3) it's easy as it's a nested loop with three levels that tests all combinations aginst the supplied string.  This extends to an "n" length password will need an "n" level nested loop - easy peasy.

e.g. for 3 character pw

aaa, aab, aac, ... , aaz, aba, abb, abc, ..., abz, aca ...  it's 27^3 = 20000 iterations but is okay.

THE QUESTION IS: could you give me a hint of how to dynamically loop an "n" length string?  Seems hard and maybe at this early stage I don't have all the tools I need.

I toyed with goto statements, while loops, etc but keep coming up against an impass. I don't know get the nested loop I need to do this.  I searched google for a few things but the only one I found was in c# and it just tested to see if passwordTest(i) = suppliedPassword(i) which is easy to do and not what I was going for.

I thought of making a 2D array of my possible characters and somehow using that 

{a, a, a

b, b, b, 

c, c, c}

I think I then run into the same issues - how do I increment one leve at a time while cycling thorugh all possibilities?

Please put me out of my misery ...

__________________

Cheers,

cbrad.)

Bryan
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!
Joined: 2011-04-22
Points: 2310

If you really wanted you could do something like this:

int maxStringLength = 5;

for (int i = 1; i <= maxStringLength; i++)
{
//start string here as ""

for (int j = 0; j < i; j++)
{
//build string to compare with here by adding characters
//you could start with char x = 'a' + j

Hope that get's you back on track! Let me know how it goes.

Bryan

cbrad
cbrad's picture
Offline
...burning like a watchful eye...I am a moderatorI've been here since the first tree. I am betaMy name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!I don't know when to shut up.We propose a toast! To you!I found a bug so fix it!
Joined: 2011-06-14
Points: 2785

Once I wrote it all down and actually assigned some variable names I realised that i needed something to tick over every time it tried all the letters.  I remember "clock" functions doing these (well, mod).

I designed a little loop to "tick" over every time the end letter went through it's 26 letters.  For that you need to establish if the last letter is multiples of 26 (straight calculation of how many iterations we are up to, "counter" with the number of letters)  with the char from the end, you need to see the whole number (counter/26^1) which is 0 until 27 and then it's 1.  1%26 = 1. so that would tick over the second char to the char(97) + 1. - complex to exmplain so here's my code ...

I'm quite the captain chuffy at the moment  ... (if you couldn't tell!)

Now, back to the "real learning" ...

Brad

 

 

int findPassWord(string actualPassWord)

{

clock_t startTime;

startTime = clock ();

int pwLength = actualPassWord.length();

string userPassWord(actualPassWord);

char* pw ;

pw = (char* )malloc(pwLength+1); //allocate the amount of memory needed by pw

//initialise the pw to all 'a's.

for (int j = 0; j < pwLength; j++) {pw[j] = char(97);}

int numPossibleChars = 26; //(a-z)

for (int counter = 0; counter < pow(numPossibleChars,pwLength); counter++)

{

// step through each char in the pasword and set it based on where we are with the coutner

for (int i = 0; i < pwLength; i++)

{

/* below is a term "counter/pow(numPossibleChars,i)%numPossibleChars" which will cycle through all chars needed (a-z for example) and then at multiples of 26, will increment the next one by 1). */

pw[i] = (char)(97+((int)(counter/pow(numPossibleChars,i))%numPossibleChars));

if (pw==userPassWord)

{

cout << "Hey Buddy, you guessed my password ... \"" << pw << "\" in " << (clock() - startTime)/1000 << " seconds" << endl;

return 0;    // is there a better way of returning??

}

}

}

free(pw);

cout << "Program Failed to get password" << endl;

return 0;

}

__________________

Cheers,

cbrad.)

FrancesRowe
FrancesRowe's picture
Offline
Joined: 2012-04-11
Points: 0

"You are taking your next big leap in your journey of becoming the greatest software developer the world has ever seen!" -

the best first post i could possibly read!

Glad I joined

___________________________________________

mp4 to avi http://freemp4toaviconverter.com/

cbrad
cbrad's picture
Offline
...burning like a watchful eye...I am a moderatorI've been here since the first tree. I am betaMy name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!I don't know when to shut up.We propose a toast! To you!I found a bug so fix it!
Joined: 2011-06-14
Points: 2785

Thanks - and welcome aboard the Wibit Train!