Hello!

I need your help, I don’t know how to be (the point is that the entered word is encrypted in accordance with the table (letter = digit), but there is such a moment that a certain number of letters = a specific number. For example, the combination "CON" = 12. If there is such a combination in the entered word, then it is replaced by 12, and not each letter separately.

The code for tracking this syllable and replacing it with a digit is:

string s1 = textBox1.Text; string s2 = "con"; bool b; b = s1.Contains(s2); if (b = s1.Contains(s2)) { result += 12; } 

The encryption code itself is:

 for (int i = 0; i < textBox1.Text.Length; i++) { foreach (DataGridViewColumn col in dataGridView1.Columns) { if (col.HeaderText == textBox1.Text[i].ToString()) { result += dataGridView1.CurrentRow.Cells[col.Index].Value; } } } label1.Text = "Результат: " + result; 

How do I properly mix these two pieces of code? And then if I write inward if (b = s1.Contains(s2)) , then after each letter I write 12 or other nonsense ...

Help me please! Thank you very much in advance!

  • one
    If I understand the problem correctly, here it is better to use the Replace () method on the line. And do not have to fence any cycles. The above replacement is written in one line s1.Replace ("CON", "12"); - nnesterov
  • one
    Ingenious encryption algorithm: Stirlitz - nervously smokes on the sidelines. - Barmaley
  • I did not quite understand how and instead of what this code goes: s1.Replace ("CON", "12"); ? if I allow add result + = s1.Replace ("con", "12"); in foreach, I do not achieve anything, because each letter is encrypted separately and after each number 12 is added ... explain pliz) I also have the condition i <textBox1.Text.Length and therefore three times 12 and added (((( wicS
  • 3
    Here just discussed managed languages ​​and here a bright example of level of entry. - avp
  • one
    The fact of the matter is that the cycle "for (int i = 0; i <textBox1.Text.Length; i ++)" is absolutely not needed. Leave one foreach cycle in the body of which you replace using Replace - nnesterov

1 answer 1

There is no studio at hand, so from the knees

 String str1 = "connect"; String str2=str1; DataGridView dgw = dataGridView1; int dgw_maxIndex = dgw.RowCount-1; int result=0; for(int i=dgw_maxIndex; i<=0;i++){ if(str2.Equals("")){ break; }else if(str2.Contains(dgw.Item[0,i].Value)){ str2.Reaplce(dgw.Item[0,i].Value,""); result += dgw.Item[1,i].Value; }else{ continue; } } 
  • there is no such item as Item in the table ... what did you mean by it? - wicS
  • Well, if he specified item [x, y], then, most likely, he had in mind a cell - teanSH
  • @wicS, @ teanISH rights, meant a cell, where [0, i] is a dictionary element, and [1, i] is its replacement - stck
  • so and how to fix it? I understand that the cell, but the studio writes that there is no such thing in this situation ( - wicS
  • @wics, I don’t know what is written there and why it doesn’t work - imagine the code - stck