Palindrome Finder Not Working

I need help debugging my code. What I want my code to do is to find the length of the largest palindrome in a string. A palindrome is something that will read the same forwards and backwards. Ex: bob or abccba.

Sample Input:
abcxyzzyx

Sample Output:
6

Right now my code can compile, but it will just spit out "0" for odd numbered strings, and will just output nothing and give an error for even numbered strings.

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
#include <iostream>
#include <string>

using namespace std;
int main(){

    string s;
    int n = 1, a = 0;
    cin >> s;

    if (s.length() %2 != 0) {
        for (int i = 0; i < s.length(); i++) {
            while (i != s.length() && s[i-n] == s[i+n]) {
                n++;
                if (n >= a) {
                    a = n *2 +1;
                }
                n = 0;
            }
        }
    }
    else {
        for (int k = 0; k < s.length(); k++) {
            while (k != s.length() && s[k-n] == s[k+n+1]) {
                n++;
                if (n >= a) {
                    a = n *2 +1;
                }
                n = 0;
            }
        }
    }

    cout << a;

    cin.get();
    return 0;

}


Thanks for reading.

Edit: I realized that whether the string's length is odd or even doesn't matter, only the palindrome's length matters.

Here's a revised version of the code that still will give errors and I don't know why:

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
#include <iostream>
#include <string>

using namespace std;
int main(){

    string s;
    int n = 1, a = 0;
    cin >> s;


        for (int i = 0; i < s.length(); i++) {
            while (i != s.length() && s[i-n] == s[i+n] || s[i-n] == s[i+n+1]) {
                n++;
                if (n >= a) {
                    a = n *2 +1;
                }
                n = 0;
            }
        }

    cout << a;

    cin.get();
    return 0;

}


Edit #2: I think I solved it.

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
#include <iostream>
#include <string>

using namespace std;

int main(){

    string s;
    int a = 0;
    
    cin >> s;
    
    for (int i = 0; i < s.length(); i++){
        int n = 1;
        for (int j = 1; i-j >= 0 && i+j < s.length(); j++){
            if (s[i-j] == s[i+j]){
                n += 2;
            }
            else {
                break;
            }
        }
        if (n >= a){
            a = n;
        }
    }

    for (int i = 0; i < s.length() -1; i++){
        int n = 0;
        for (int j = 0; i-j >=0 && i+j < s.length(); j++){
            if (s[i-j] == s[i+j+1]){
                n += 2;
            }
            else {
                break;
            }
        }
        if (n >= a){
            a = n;
        }
    }

    cout << a;

    cin.get();
    return 0;
    
}


Thanks for all the responses.
Last edited on
s[i+n] and s[i+n+1] will lead to an index out bounds error.
Topic archived. No new replies allowed.