Palindrome Checker: not running correctly

This program should reverse a string and be able to check if a word is a palindrome. Everything besides the palindrome checker is working. Any suggestions?




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
  1 #include <iostream>
  2 #include <string>
  3
  4 using namespace std;
  5
  6 int main();
  7
  8 string to_lower(string word, int x){
  9    if ( x <= word.length()){
 10       word[x] = tolower(word[x]);
 11       to_lower(word, x+1);
 12    }
 13    else;
 14    return word;
 15 }
 16
 17 string check_alpha(string word, int k){
 18    if (k < word.length()){
 19       if (isalpha(word[k])){
 20          cout <<"It adds an x here"<< k << endl;
 21          k++;
 22          check_alpha(word, k);
 23       }
 24       else;
 25             cout<<"It should erase a letter"<<k<<endl;
 26             word.erase(k,1);
 27             check_alpha(word, k);
 28    }
 29    else;
 30         return word;
 31 }
 32
 33 string check_palindrome(string word, string reversed, int k, int &check){
 34    if (k <= word.length()/2.0){
 35       if (word[k] != reversed[k]){
 36          cout<< "You did not enter a palindrome"<<endl;
 37          check = 0;
 38       }
 39    }
 40 }
 41
 42 void determine_palindrome(){
 43    int k = 0;
 44    string word;
 45    string reversed;
 46    int check = 1;
 47    cin.ignore();
 48    cout <<"Enter your string: ";
 49    getline(cin, word);
 50
 51    //makes word lowercase
 52    word = to_lower(word, 0);
 53
 54    //removes non-letter characters
 55    word = check_alpha(word, k);
 56    cout << word << endl << k << endl;
 57
 58    //reverses a string
 59    reversed = string(word.rbegin(), word.rend());
 60
 61    //determines if word is a palindrome
 62    check_palindrome(word, reversed, 0, check);
 63    if (check == 1)
 64       cout << "You entered a palindrome!" << endl;
 65    main();
 66
 67 }
 68
 69
 70 void reverse_string(){
 71    string word;
 72
 73    cin.ignore();
 74    cout <<"Enter your string: ";
 75    getline(cin, word);
 76    //reverse word
 77    word = string(word.rbegin(), word.rend());
 78    cout<< "Reversed string: " << word << endl;
 79    main();
 80 }
 81
 82
 83
 84 int main(){
 85
 86    int choice;
 87
 88         cout << "Do you want to reverse a string, check if it is a palindrome, orquit?/n(Enter 1 for reverse, 2 for a palindrome, and anything else to quit)";
 89         cin >> choice;
 90
 91         if (choice == 1)
 92            reverse_string();
 93         else
 94            return 0;
 95 }


well one suggestion just from the start is dont call main() from within a function... that just doesnt make sense at all.

the determine_palindrome function doesn't get called anywhere and when one is added it prints out a bunch of unneeded crap from
1
2
3
4
5
6
 20          cout <<"It adds an x here"<< k << endl;
 21          k++;
 22          check_alpha(word, k);
 23       }
 24       else;
 25             cout<<"It should erase a letter"<<k<<endl;


Last edited on
You are only checking the first char in your word and in the reversed string.

Also, I don't know how you determined the value of k. Does it have any special thing it does or is it just an offset to the first letter of the word?

line 78:
word = string(word.rbegin(), word.rend());

Where did you declare this function? And why does it have the name of a type?
And why have a header for main at the top (line 6)?
Topic archived. No new replies allowed.