【问题标题】:Brut force add characters from array? how to [closed]蛮力从数组中添加字符?如何[关闭]
【发布时间】:2016-01-06 21:41:34
【问题描述】:

我在 try catch 和 final 块中有一个函数我知道该函数将始终使用 finally 代码块但在这里它应该添加字符 我不想使用 while 循环

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

现在如何制作一个函数,该函数第一次使用声明为 increase_me 的字符串,并在最终从第二个数组等失败时将其值增加到 26 max

【问题讨论】:

  • 如果你想编写一个方法,在每次调用时将字符串移动到下一个排列,你必须检查当前字符串的最后一个字符(例如"asd123B!""!"),并实施翻转("B+" 变为 "Ca")。这是可行的,但我希望你不要指望我们为你编写那个方法。
  • 你知道这会因为无限递归导致堆栈溢出问题吗?
  • 很难说你在问什么,但我认为你只是在寻找类似于“Generate all combinations for a list of strings”的东西
  • 如果目的是将每个数组 small、cap、num 和 spcl 中的字符串添加到 increse_me,可以只使用每个数组上的 Join 方法添加到 increase_me假设需要一个大宗交易。如果一个一个地重复执行每个字符串,那么可能需要一个循环,除非人们想编写一些非常丑陋的递归函数来传递不断缩小的数组以添加可能采用的另一条路线的值。

标签: c# .net combinations brute-force


【解决方案1】:

在直观的层面上,您可以只传入一个数组和字符串,然后返回一个更新后的字符串,并附加第一个元素:

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

这将使brut() 必须聪明地知道将什么传递给函数,这样它就不会传入空数组,尽管如果是这种情况它只会返回第一个字符串。当然,这只是整体解决方案的一部分,因为我怀疑正则表达式在这里是一个更容易使用的解决方案,但没有被问到。

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多