0

i have a function in try catch and final block i know that function will always use finally block of code but here it should add character i no want to use while loop

 string increse_me ;
 public void brut()
 {
     string[] small = new string[] { "a", "b", "c", "d", "e", "f", "g",   "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
     string[] cap = new  string []  {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
     string[] num = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
     string[] spcl = new string[] { "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ">", "<", "+" };

   if( textbox1.Text == increase_me)
   {
       messagebox.show("matched")
   }
  catch (exception ex) 
  {
  }  
  finally 
  {  
      brut();
     //here i have to call function again with increase letter as start from "a"it add next to it
  }
}

Now how to make a function that use string declared increase_me for the first time and also increase its value when finally fails from second arrray and so on to 26 max digit

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
office 302
  • 196
  • 1
  • 13
  • 1
    If you want to write a method that moves the string the next permutation with each call, you'll have to check the last character of the current string (e.g. `"!"` of `"asd123B!"`), and implement rollover (`"B+"` becomes `"Ca"`). That's doable, but I hope you don't expect us to write that method for you. – CodeCaster Jan 06 '16 at 21:45
  • 3
    You are aware that this will have stack overflow problems because of an infinite recursion right? – JB King Jan 06 '16 at 21:45
  • 1
    It is hard to tell what you are asking, but I think you are just looking for something similar to "[Generate all combinations for a list of strings](http://stackoverflow.com/questions/10515449/generate-all-combinations-for-a-list-of-strings)" – Scott Chamberlain Jan 06 '16 at 21:45
  • 1
    Ever look at the `String.Join` method? https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx – JB King Jan 06 '16 at 21:48
  • If the intent is to add the strings from each of the arrays of small, cap, num and spcl to the `increse_me`, one could just use the Join method on each array to add into the increase_me assuming a block transaction is desired. If there is repetition of doing each string one by one then a loop is likely to be required unless one wants to write some really ugly recursive function to pass in the ever shrinking array to add the values across which would be another route one could take. – JB King Jan 06 '16 at 21:51
  • actually i have many more than 100 database for opening connection of lost password become problem so event goes to finally bock here i have to manage and add charecter and again call the same function with next password if fails then in finally again add charecter try new one and so on till password match – office 302 Jan 06 '16 at 21:52

1 Answers1

0

On an intuitive level, you could just pass in an array and the string and return an updated string with the first element appended:

string stringUpdate(string update, string[] toAdd) 
{
     if (toAdd!=null && toAdd.length>0) {
          return update+toAdd[0];
     }
     return update;
}

This would then make brut() have to be smart about what to pass into the function so that it isn't passing in empty arrays though it would just return the first string if that is the case. Course this is just part of the overall solution as I would suspect a regular expression to be a much easier solution to have here but that wasn't asked.

JB King
  • 11,860
  • 4
  • 38
  • 49