Urgent Help if Possible - Palindrome Problem.

First off, I made this code in VB.NET as that's the language I'm the most familiar with, but the professor wants this problem to be solved in c++ so best I could do in such a short time is to convert it online to C#. But I think it shouldn't be hard to understand since C# syntax is not very different from C++ as far as I know. My code is below, my problem is this:

I'm having a very difficult task. 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).

ALLAH - HALLAH WRONG, the output is 4 chars when all it needs is just one.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

public class Form1
{


	private void Button1_Click(System.Object sender, System.EventArgs e)
	{
		int counter = 0;
		int counter2 = 0;
		int temp = 0;
		int Half = 0;
		bool Test = false;
		RichTextBox1.Text = RichTextBox1.Text.Replace(" ", "");
		RichTextBox1.Text = RichTextBox1.Text.Replace(".", "");
		RichTextBox1.Text = RichTextBox1.Text.Replace(Constants.vbLf, "");

		int j = RichTextBox1.Text.Length;

		if (RichTextBox1.Text.Length % 2) {
			// just for help to test MsgBox("You've put an odd no. of Chars.")
			Half = Math.Floor((RichTextBox1.Text.Length - 1) / 2);


			for (i = 0; i <= Half - 1; i++) {
				for (j = RichTextBox1.Text.Length - 1; j >= 0; j += -1) {
					if (i < Half) {
						if (RichTextBox1.Text.Chars(i) == RichTextBox1.Text.Chars(j)) {
							Interaction.MsgBox(RichTextBox1.Text.Chars(i) + RichTextBox1.Text.Chars(j));
							counter = counter + 2;
						} else {
							Interaction.MsgBox("Same chars at respective places are " + counter);
							Test = true;
							Interaction.MsgBox("Test became true");
							break; // TODO: might not be correct. Was : Exit For
						}
						i += 1;
					} else {
						//i += 1
					}
					// i = i + 1
				}

				if (Test == true)
					break; // TODO: might not be correct. Was : Exit For

			}

		} else {
			// just for help to test  MsgBox("You've put an even no. of Chars.")
			Half = Math.Floor((RichTextBox1.Text.Length) / 2);
			Interaction.MsgBox(Half);


			for (i = 0; i <= Half - 1; i++) {
				for (j = RichTextBox1.Text.Length - 1; j >= Half; j += -1) {
					if (i < Half) {
						if (RichTextBox1.Text.Chars(i) == RichTextBox1.Text.Chars(j)) {
							//            MsgBox(RichTextBox1.Text.Chars(i) & RichTextBox1.Text.Chars(j))
							counter = counter + 2;

						} else {
							Interaction.MsgBox("Same chars at respective places are " + counter);
							Test = true;
							Interaction.MsgBox("Test became true");
							break; // TODO: might not be correct. Was : Exit For
							//   j -= 1
						}
						i += 1;
						//   Exit For

					}

				}

				if (Test == true)
					break; // TODO: might not be correct. Was : Exit For

			}

		}

		if (counter / 2 == Half) {
			Interaction.MsgBox("This text is a palindrome, it doesn't need any additional character.");

		} else {
			//     For j = RichTextBox1.Text.Length - 1 To 1 Step -1
			//If RichTextBox1.Text.Chars(j) = RichTextBox1.Text.Chars(j - 1) Then

			for (i = 0; i <= RichTextBox1.Text.Length - 2; i++) {
				if (RichTextBox1.Text.Chars(i + 1) == RichTextBox1.Text.Chars(i)) {
					counter2 = counter2 + 1;
					Interaction.MsgBox("The no. of same straight chars atm is " + counter2);
					if (temp <= counter2) {
						temp = counter2;
						Interaction.MsgBox("Biggest same straight char no. is " + temp);
					}

					Interaction.MsgBox(counter2);
				} else {
					counter2 = 0;
				}

			}
			if (counter2 <= temp) {
				counter2 = temp;
			}
			Interaction.MsgBox("Temp =" + temp);

			Interaction.MsgBox("This text is not a palindrome, it needs " + (RichTextBox1.Text.Length - 1 - counter - temp) + " chars to become one.");
		}
		//MsgBox(counter)

	}

}


Just a tip in c++, you could use the string reverse function from the string library and then compare the two strings, if the same then its a palindrome!
Yeah I heard of that too, but it's just a shorter way of checking if the text is a palindrome or not which is not the main concern from me. Thanks for the tip anyway!
I'm not familiar with the problem, but there might be a simpler solution algorithmically

1
2
3
4
5
6
7
8
int GetPalindromeLength(const char * pStr) // Null terminated string
{
    if (pStr[0] == '\0' || strcmp(pStr, reverse(pStr) == 0)  If string is empty or already a palindrome, we dont need any more characters for this to be a palindrome
         return 0;


    return 1 + (GetPalindromeLength(&pStr[1]);
}
There is an algorithm that finds the minimum number of chars if I am allowed to add from beginning or end, but I can't figure an algorithm for when adding chars from within the text
Topic archived. No new replies allowed.