Palindrome de-bug!

I have this program that I have basically done besides that there is still 2 errors that I am getting. The program is supposed to tell me if a sentence is a palindrome or not, Ex: A man, a plan, a canal, panama -- this is one. The program should take out everything besides the letters and make them all the same case. This is the code I have now. Please help with the bugs! Thank You!

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  

/*-----------------------------------------------------------------------------------------------------------
        
 Problem 1: The program will ask the user to enter a sequence of scores between 0 and 100, inclusive. The
    program then will print out the stats about these scores, including: the number of scores, the maximum 
    score, the minimum score and the average score.
 
 Problem 2: The program will ask the user to enter a sentence. The program will then display a message to 
    indicate if this sentence is a palindrome.  The following sentence is a palindrome: A nut for a jar of 
    tuna. The white space and non English letters are not counted. The case difference is ignored.
 
 -----------------------------------------------------------------------------------------------------------*/

#include <iostream>
#include <vector>
#include <string>
#include <cctype>

using namespace std;

bool is_palindrome(string sentence);
void get_scores(vector<int> &v);
void print_stats(vector<int> &v);
string reverse(const string& s);
string make_lower(const string& s);
void swap(char& v1, char& v2);
string remove_punct(const string& s, const string& punct);

int main()
{
    cout << "Enter scores between 0 and 100. Enter a negative to stop." << endl;
    
    vector<int> u;
    get_scores(u);
    print_stats(u);
    
    cout << "Enter a sentence to tell if it is a palindrome: " << endl;
    
    string str;
    getline(cin, str);
        if ( is_palindrome(str) )
            cout << "\"" << str + "\" is a palindrome!!" << endl;
        else
            cout << "\"" << str + "\" is NOT a palindrome!!" << endl;
    return 0;
}
void swap(char& v1, char& v2)
{
    char temp = v1;
    v1 = v2;
    v2 = temp;
}

string reverse (const string& s)
{
    int start = 0;
    int end = s.length();
    string temp(s);
    
    while (start < end)
    {
        end--;
        swap(temp[start], temp[end]);
        start++;
    }
    return temp;
}

string make_lower(const string& s)
{
    string temp(s);
    for (int i = 0; i < s.length(); i++)
        temp[i] = tolower(s[i]);
    return temp;
}

string remove_punct(const string& s, const string& punct)
{
    string no_punct;
    int s_length = s.length();
    int punct_length = punct.length();
    
    for( int i = 0; i < s_length; i++)
    {
        string a_char = s.substr(i,1);
        int location = punct.find(a_char, 0);
        if(location < 0 || location >= punct_length)
            no_punct = no_punct + a_char;
    }
    return no_punct;
}


bool is_palindrome(const string& s)
{
    string punct ( ".;:.?!'\" ");
    string str(s);
    str = make_lower(str);
    string lower_str = remove_punct(str, punct);
    return (lower_str == reverse(lower_str));
}

void get_scores(vector<int> &v)
{
    int input;

    while (input > 0)
    {
        cin >> input;
        v.push_back(input);
    }
}

void print_stats(vector<int> &v)
{
    int max = 0, min = 101, total = 0;
    double average;
    cout << "Scores are: ";
    for(unsigned int i = 0; i < v.size(); i++)
    {
        cout << v[i] << ", ";
        
            if ( v[i] > max )
            {
                max = v[i];
            }
            if (v[i] > 0)
            {
                if (v[i] < min)
                {
                    min = v[i];
                }
                total = total + v[i];
            }
    }
    
    //cout << "Stats entered: " << v[] << endl;
    average = total / (v.size() - 1);
    cout << " " << endl;
    //cout << "Stats entered: " <<  << endl;
    cout << "Max = " << max << endl;
    cout << "Min = " << min << endl;
    cout << "Total = " << total << endl;
    cout << "Average = " << average << endl;
}
Seems like it would be more effective to just ignore the non-alphabetic characters and then to compare front to back. Than to actually remove the non-alphabetic characters. Also if you decide to do it your way. On line 95 you need to pass by copy or make the prototype on line 22 reference. By the way there is a new line left in buffer from the cin >> and on line 41 when you use getline it is going to read until a new line is found. So you will want to ignore anything left in the buffer before calling that. You can do something like cin.get(); or better cin.ignore(1024, '\n'); or better yet cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); Also, on line 97 you are missing ',' though, I personally would just use the !isalpha(someCharacter) or ispunct(someCharacter)

I personally would just have an iterator to the start and one at the end and then compare them (if a non-alpha is found increment until one is found) and work towards the middle.
Topic archived. No new replies allowed.