So, I'm having a very difficult task here. I need to analyze a certain text and find the minimum number of characters it needs to become a palindrome. The text will not have any special characters or numbers, just alphabet letters. Now, I've made some progression and have tried a lot of situations correctly, but in some situations I've gotten wrong outputs on the MINIMUM number of characters needed. I've tested the below texts:

AAABBBBCCCCC - AAABBBBCCCCCBBBBAAA ✓ 7
CCCCCBBBBAAA - AAABBBBCCCCCBBBBAAA ✓ 7
BBBBCCCCCAAA - AAABBBBCCCCCBBBBAAA ✓ 7
FASLLI - FAS-I-LLI-SAF ✓ 4
ARBEN - ARBEN-EBRA ✓ 4
ARBENA - ARBEN-EBR-A ✓ 3
ARBANA - ARBAN-ABR-A ✓ 3
ALL - ALL-A ✓ 1
ALLLL - ALLLL-A ✓ 1
BAOUB - BAOU-OA-B ✓ 2
ASDRFFF - ASDRFFF-DRSA ✓ 4
AAABBB - AAABBB-AAA / BBB-AAABBB ✓ 3
ARBENARBEN - ARBENARBEN-EBRANEBRA ✓ 9
ALLALL - ALLALL-A WRONG , outputs "4" when all there's needed is 1.
AAABBBBAAABBBBCCCCC - CCCCCBBBB-AAABBBBAAABBBBCCC WRONG, outputs 14 when all there's needed is 9 chars. In this case there should be noted that there are 2 palindromes within the text (as in substrings which may have something to do with the wrong output that I get, but I can't find what it is).

If anyone can help me with what is going wrong with the 2 cases in which the output is wrong I would appreciate it very very much. My code is below.

vb Code:
  1. Public Class Form1
  2.  
  3.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  4.  
  5.         Dim counter As Integer = 0, counter2 As Integer = 0, temp As Integer = 0
  6.         Dim Half As Integer
  7.         Dim Test As Boolean = False
  8.         RichTextBox1.Text = RichTextBox1.Text.Replace(" ", "")
  9.         RichTextBox1.Text = RichTextBox1.Text.Replace(".", "")
  10.         RichTextBox1.Text = RichTextBox1.Text.Replace(vbLf, "")
  11.  
  12.         Dim j As Integer = RichTextBox1.Text.Length
  13.  
  14.         If RichTextBox1.Text.Length Mod 2 Then
  15.             ' just for help to test MsgBox("You've put an odd no. of Chars.")
  16.             Half = Math.Floor((RichTextBox1.Text.Length - 1) / 2)
  17.  
  18.             For i = 0 To Half - 1
  19.  
  20.                 For j = RichTextBox1.Text.Length - 1 To 0 Step -1
  21.                     If i < Half Then
  22.                         If RichTextBox1.Text.Chars(i) = RichTextBox1.Text.Chars(j) Then
  23.                             MsgBox(RichTextBox1.Text.Chars(i) & RichTextBox1.Text.Chars(j))
  24.                             counter = counter + 2
  25.                         Else
  26.                             MsgBox("Same chars at respective places are " & counter)
  27.                             Test = True
  28.                             MsgBox("Test became true")
  29.                             Exit For
  30.                         End If
  31.                         i += 1
  32.                     Else
  33.                         'i += 1
  34.                     End If
  35.                     ' i = i + 1
  36.                 Next
  37.  
  38.                 If Test = True Then Exit For
  39.  
  40.             Next
  41.  
  42.         Else
  43.           ' just for help to test  MsgBox("You've put an even no. of Chars.")
  44.             Half = Math.Floor((RichTextBox1.Text.Length) / 2)
  45.             MsgBox(Half)
  46.  
  47.             For i = 0 To Half - 1
  48.  
  49.                 For j = RichTextBox1.Text.Length - 1 To Half Step -1
  50.                     If i < Half Then
  51.                         If RichTextBox1.Text.Chars(i) = RichTextBox1.Text.Chars(j) Then
  52.                             '            MsgBox(RichTextBox1.Text.Chars(i) & RichTextBox1.Text.Chars(j))
  53.                             counter = counter + 2
  54.  
  55.                         Else
  56.                             MsgBox("Same chars at respective places are " & counter)
  57.                             Test = True
  58.                             MsgBox("Test became true")
  59.                             Exit For
  60.                             '   j -= 1
  61.                         End If
  62.                         i += 1
  63.                         '   Exit For
  64.  
  65.                     End If
  66.  
  67.                 Next
  68.  
  69.                 If Test = True Then Exit For
  70.  
  71.             Next
  72.  
  73.         End If
  74.  
  75.         If counter / 2 = Half Then
  76.             MsgBox("This text is a palindrome, it doesn't need any additional character.")
  77.         Else
  78.  
  79.             '     For j = RichTextBox1.Text.Length - 1 To 1 Step -1
  80.             'If RichTextBox1.Text.Chars(j) = RichTextBox1.Text.Chars(j - 1) Then
  81.  
  82.            For i = 0 To RichTextBox1.Text.Length - 2
  83.                 If RichTextBox1.Text.Chars(i + 1) = RichTextBox1.Text.Chars(i) Then
  84.                     counter2 = counter2 + 1
  85.                     MsgBox("The no. of same straight chars atm is " & counter2)
  86.                     If temp <= counter2 Then
  87.                         temp = counter2
  88.                         MsgBox("Biggest same straight char no. is " & temp)
  89.                     End If
  90.  
  91.                     MsgBox(counter2)
  92.                 Else
  93.                     counter2 = 0
  94.                     End If
  95.  
  96.             Next
  97.             If counter2 <= temp Then
  98.                 counter2 = temp
  99.             End If
  100.             MsgBox("Temp =" & temp)
  101.  
  102.             MsgBox("This text is not a palindrome, it needs " & (RichTextBox1.Text.Length - 1 - counter - temp) & " chars to become one.")
  103.         End If
  104.         'MsgBox(counter)
  105.  
  106.     End Sub
  107.  
  108. End Class