Several Functions are built wrong for Palindrome

Pretty new at this and i am in need of help. could you guide me on how to fix each of these functions. i am not allowed to use arrays or anything from the <algorithm> library.

this code is going to look at a word and reverse it with no spaces or punctuation. then see if its a palindrome and if not give the location and letters needed to make it to a palindrome.



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

using namespace std;

string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);

int main()
{
    string line;
    
    while (getline (cin, line) )
    {
    	cout<<"Original string: " << line<< endl;
    	cout<<"Processed string: " << process(line)<< endl;
    	
        if (is_palindrome(line))
            cout<<"Line is a palindrome";
        else
        {
            cout<<"Line is NOT a palindrome";
            cout<<"Characters to insert at location "<<palindrome_fix_location(line)
                <<"are "<<palindrome_addition( process(line), palindrome_fix_location(line));
        }
            
    }
    
    return 0;
}

/*
    returns a string such that it is in lower case and contains no
    punctuation or spacing
*/

string process(string edited)
{
    	for (unsigned int i = edited.size() - 1 ; i >= 0 ; i--)
	        if (isalnum(edited[i]))
	             tolower(edited[i]);
	   return edited;

}


bool is_palindrome(string edited)
{
    if(edited.size() < 2)
        return true;
    if(edited[0] != edited[edited.size()-1])
        return false;
    
    return is_palindrome(edited);
        
    /*
    should use a recursive ​method to determine if the string is a
    palindrome
        Remember that a recursive solution has both a base case (or cases) and a recursive
        case (or cases) and you must decide which to use using some sort of logic.
    
    */
    
}


int palindrome_fix_location(string edited)
{
    int count;
        for(unsigned int i = 0, j = edited.size() -1; 
        i < (edited.size() / 2); i++, j--)
        if (edited[i] != edited[j])
            return count++;
    /*
        should be as described below:
○ This function should return a location where some number of characters could be added
to a non-palindrome string to make it a palindrome.
○ There are many ways to do this, for example the string aabcaa can be made into a
palindrome by simply reversing and duplicating the string at position 0 (before the string)
or position 6 (after the string), but a more elegant solution would determine that you could
just add the character c at location 2.
○ An optimal solution will determine the minimum number of characters to add to the string.
You do not need to find the optimal solution but if you do then you will receive 1 extra
credit PD point. Likewise, PD deductions will result if your program always reports that it
should simply reverse the entire string (or the entire string minus one character) and put it
at the beginning or end.
    */
}

string palindrome_addition(string edited, int n)
{
        for(unsigned int i = 0, j = edited.size() -1; 
        i < (edited.size() / 2); i++, j--)
        
        if (edited[i] != edited[j])
            static_cast<char>(tolower(edited[i]));
            
        return edited;
    /*
 given a string and the location where a
 string should be added to make it a palindrome, this should return the text that needs to be added
 at that location
    */
}
> how to fix each of these functions
first, determine what's wrong with them
not going to bother if you don't at least provide the symptoms
ne555,

Thanks for the reply, I'm trying to use an online IDE and all it outputs is cout<<"Original string: " << line<< endl



Then it terminates

Last edited on
|| foo.cpp: In function ‘std::__cxx11::string process(std::__cxx11::string)’:
foo.cpp|43 col 44| warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits]
||   for(unsigned int i = edited.size() - 1; i >= 0; i--)
||                                           ~~^~~~
$ gdb a.out
(gdb) run
input
Original string: input

Program received signal SIGSEGV, Segmentation fault.
0x0000555555558d3d in process (edited="input") at foo.cpp:44
44			if(isalnum(edited[i]))
(gdb) print i
$1 = 4294967295
ok so im still really lost. if i test that fuction out it works in this context below.



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

using namespace std;

int main() {
    string name;

	cout<<"Enter a String"<<endl;
	getline(cin, name);
	
	for (int i = name.size() - 1 ; i >= 0 ; i--)
	    if (isalnum(name[i]))
	  
	        cout<<static_cast<char>(tolower(name[i]));
	        
	  
	       
	    
	    cout<<endl;
	    
	    
 return 0;
 
}
this code is going to look at a word and reverse it with no spaces or punctuation
You have no code that removes spaces.

I think you'll find that the easiest way to process the string is to go through the original string, copying processed letters to a new string and skipping non-letters. Then return the new string.

Read the comments on lines 61-64. I suggest a function like this:
// Return true if "str" is a palindrome in the interval [start, stop). To check
// a whole string, call it is_palindrome(str, 0, str.size())
1
2
3
4
5
6
7
8
9
bool is_palindrome(const string &str, size_t start, size_t stop)
{
    if (base_case) {
         return something;
    } else {
        // Do the recursive case
        // return something
    }
}


As an aside, I recommend that you always code recursive functions using this pattern. It makes the code clear and it forces you to think about the base case. It's easy to forget about it, as you've found :)

Line 45 converts the character to lower case and throws away the result. It should be
edited[i] = tolower(edited[i]);

Lines 77: count is used before it's uninitialized.

I see a pattern where the assignment or the comments in the code (presumably from the professor), haven't been followed. You should read the comments carefully and be sure that you're doing what they ask. It will make writing the code easier and you'll get a higher grade.
Thank you dhayden,

the functions that have been added are listed in that manner due to the rules of the assignment.

"You will write the following functions (prototypes given) and no others​"


it runs now but a few of the functions are still not working for me.

here is the wrong output im getting.


Original string: lappal
Processed string: lappal
Line is a palindrome

Original string: A man, a plan, a canal, Panama!
Processed string: A man, a plan, a canal, Panama!
Line is a palindrome

Original string: SER@&!*IO DRkdk!@#
Processed string: SER@&!*IO DRkdk!@#
Line is a palindrome

Original string: AKJSJDHG
Processed string: AKJSJDHG
Line is a palindrome




here is the updated code.

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

using namespace std;

string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);

int main()
{
    string line;
    
    while (getline (cin, line) )
    {
    	cout<<"Original string: " << line<< endl;
    	cout<<"Processed string: " << process(line)<< endl;
    	
       if (is_palindrome(line))
           cout<<"Line is a palindrome"<< endl;
    else
       {
           cout<<"Line is NOT a palindrome"<< endl;
           cout<<"Characters to insert at location "<<palindrome_fix_location(line)
               <<" are "<<palindrome_addition( process(line), palindrome_fix_location(line));
            cout<<endl;
        }
        
        cout<<endl;    
    }
    
    return 0;
}

/*
    returns a string such that it is in lower case and contains no
    punctuation or spacing
*/

string process(string edited)
{
    string newstr;
    	for (unsigned int i = edited.size() - 1 ; i <= 0 ; i--)
	        if (isalnum(edited[i]))
	        {
	             edited[i] = tolower(edited[i]);
	        }
	  return edited;

}


bool is_palindrome(string edited)
{
    int start = 0;
    int end = edited.size() - 1;
    
    if(start < end)
        return true;
    if(edited[start++] != edited[end--])
        return false;

    return is_palindrome(edited);
    
}


int palindrome_fix_location(string edited)
{
    int count;
        for(unsigned int i = 0, j = edited.size() -1; 
        i < (edited.size() / 2); i++, j--)
        if (edited[i] != edited[j])
            return ++count;
    /*
        should be as described below:
○ This function should return a location where some number of characters could be added
to a non-palindrome string to make it a palindrome.
○ There are many ways to do this, for example the string aabcaa can be made into a
palindrome by simply reversing and duplicating the string at position 0 (before the string)
or position 6 (after the string), but a more elegant solution would determine that you could
just add the character c at location 2.
○ An optimal solution will determine the minimum number of characters to add to the string.
You do not need to find the optimal solution but if you do then you will receive 1 extra
credit PD point. Likewise, PD deductions will result if your program always reports that it
should simply reverse the entire string (or the entire string minus one character) and put it
at the beginning or end.
    */
}

string palindrome_addition(string edited, int n)
{
        for(unsigned int i = 0, j = edited.size() -1; 
        i < (edited.size() / 2); i++, j--)
        
        if (edited[i] != edited[j])
            static_cast<char>(tolower(edited[i]));
            
        return edited;
    /*
 given a string and the location where a
 string should be added to make it a palindrome, this should return the text that needs to be added
 at that location
    */
}
Last edited on
closed account (E0p9LyTq)
You are going out of bounds with your for loop, you should use std::string's iterator operators.
http://www.cplusplus.com/reference/string/basic_string/

If you want to walk forward through a string, printing out each individual element:
1
2
3
4
for (auto i = aString.begin(); i != aString.end(); i++)
{
   std::cout << *i << ' ';
}


If you want to walk backwards through a string:
1
2
3
4
for (auto i = aString.rbegin(); i != aString.rend(); i++)
{
   std::cout << *i << ' ';
}

Using the string's iterators makes it harder to go out of bounds when accessing individual elements.

Rewriting your function to remove non-alpha-numeric character (including white space):
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string Process(std::string line)
{
   std::string temp;

   for (auto i = line.begin(); i != line.end(); i++)
   {
      if (std::isalnum(*i))
      {
         temp += std::tolower(*i);
      }
   }
   return temp;
}

Using parentheses makes reading code much easier to spot errors. Indentation is white space and the compiler ignores white space.

Using std::string's iterators makes reversing a string easy:
1
2
3
4
5
6
7
8
9
10
11
std::string Reverse(std::string line)
{
   std::string reversed = "";

   for (auto i = line.rbegin(); i != line.rend(); i++)
   {
      reversed += *i;
   }

   return reversed;
}

Now you simply compare the two processed strings for equality. They match, you have a palindrome.
125 test
Is NOT a palindrome

Madam I'm Adam
Is a palindrome
closed account (E0p9LyTq)
Checking two strings for equality (palindrome):
1
2
3
4
bool IsPalindrome(const std::string line, const std::string reversed)
{
   return (line == reversed);
}

My main():
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
#include <iostream>
#include <string>
#include <cctype>

std::string Process(std::string);
std::string Reverse(std::string line);
bool IsPalindrome(const std::string, const std::string);

int main()
{
   std::string line;

   while (std::getline(std::cin, line))
   {
      if (line == "QUIT")
      {
         break;
      }

      std::string lowStr = Process(line);

      std::string reversed = Reverse(lowStr);

      if (IsPalindrome(lowStr, reversed))
      {
         std::cout << "Is a palindrome\n\n";
      }
      else
      {
         std::cout << "Is NOT a palindrome\n\n";
      }
   }
}
down to a few incorrect counts, if you could help fix these please?

my incorrect output


Original string: lappal
Processed string:  lappal
Line is NOT a palindrome
Characters to insert at location 3 are  la
Final line:  la lappal

Original string: lapal
Processed string:  lapal
Line is NOT a palindrome
Characters to insert at location 3 are  la
Final line:  la lapal

Original string: A man, a plan, a canal, Panama!
Processed string:  amanaplanacanalpanama
Line is NOT a palindrome
Characters to insert at location 11 are  amanaplana
Final line:  amanaplana amanaplanacanalpanama

Original string: lap
Processed string:  lap
Line is NOT a palindrome
Characters to insert at location 2 are  l
Final line:  l lap

Original string: alapa
Processed string:  alapa
Line is NOT a palindrome
Characters to insert at location 3 are  al
Final line:  al alapa

Original string: bath
Processed string:  bath
Line is NOT a palindrome
Characters to insert at location 2 are  b
Final line:  b bath

Original string: aabcaa
Processed string:  aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are  a
Final line:  a aabcaa

Original string: abc...123...cba
Processed string:  abc123cba
Line is NOT a palindrome
Characters to insert at location 5 are  abc1
Final line:  abc1 abc123cba


The correct output i should get.


Original line: lappal
Processed line: lappal
Line is a palindrome

Original line: lapal
Processed line: lapal
Line is a palindrome

Original line: A man, a plan, a canal, Panama!
Processed line: amanaplanacanalpanama
Line is a palindrome

Original line: lap
Processed line: lap
Line is NOT a palindrome
Characters to insert at location 0 are pa
Final line: palap

Original line: alapa
Processed line: alapa
Line is NOT a palindrome
Characters to insert at location 1 are pa
Final line: apalapa

Original line: bath
Processed line: bath
Line is NOT a palindrome
Characters to insert at location 0 are hta
Final line: htabath

Original line: aabcaa
Processed line: aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are c
Final line: aacbcaa

Original line: abc...123...cba
Processed line: abc123cba
Line is NOT a palindrome
Characters to insert at location 3 are 32
Final line: abc32123cba


my working but incorrectly code.

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

using namespace std;

string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);

int main()
{
    string line;
    
    while (getline (cin, line) )
    {
    	cout<<"Original string: " << line<< endl;
    	cout<<"Processed string: " << process(line)<< endl;
    	
       if ( is_palindrome(process(line)) )
           cout<<"Line is a palindrome"<< endl;
       else 
        {
           cout<<"Line is NOT a palindrome"<< endl;
           cout<<"Characters to insert at location "<<palindrome_fix_location(process(line))
               <<" are "<<palindrome_addition( process(line), palindrome_fix_location(process(line)))
               <<endl;
           cout<<"Final line: "<<palindrome_addition( process(line), palindrome_fix_location(process(line)))
               + process(line)<<endl;
        }
        
        cout<<endl;    
    }
    
    return 0;
}

/*
    returns a string such that it is in lower case and contains no
    punctuation or spacing
*/

string process(string edited)
{
    string newstr = " ";
    	for (unsigned int i = 0 ; i < edited.size()  ; i++)
	        if (isalnum(edited[i]))
	        {
	            newstr += tolower(edited[i]);
	        }
	  return newstr;

}


bool is_palindrome(string edited)
{
    string newstr = process(edited);
    int start = 0;
    int end = newstr.size();
    
    if(start >= end)
        return true;
    if(newstr[start++] != newstr[--end])
        return false;

    return is_palindrome(newstr);
    
}


int palindrome_fix_location(string edited)
{
    int count = 0;
        for(unsigned int i = 0, j = edited.size() -1; i < (edited.size() / 2); i++, j--)
        if (edited[i] != edited[j])
             ++count;
            
        return count;
    /*
        should be as described below:
○ This function should return a location where some number of characters could be added
to a non-palindrome string to make it a palindrome.
○ There are many ways to do this, for example the string aabcaa can be made into a
palindrome by simply reversing and duplicating the string at position 0 (before the string)
or position 6 (after the string), but a more elegant solution would determine that you could
just add the character c at location 2.
○ An optimal solution will determine the minimum number of characters to add to the string.
You do not need to find the optimal solution but if you do then you will receive 1 extra
credit PD point. Likewise, PD deductions will result if your program always reports that it
should simply reverse the entire string (or the entire string minus one character) and put it
at the beginning or end.
    */
}

string palindrome_addition(string edited, int n)
{
    string letter;
        for(unsigned int i = 0, j = edited.size() -1; i < (edited.size() / 2); i++, j--)
            if (edited[i] != edited[j])
                 letter += edited[i];
            
            
        return letter;
    /*
 given a string and the location where a
 string should be added to make it a palindrome, this should return the text that needs to be added
 at that location
    */
}
Processed string:  lappal
Line is NOT a palindrome

So clearly is_palindrome() is wrong. Considering this:
You will write the following functions (prototypes given) and no others​"
and this:
[is-palindrome()] should use a recursive ​method to determine if the string is a palindrome
, I think the algorithm should be something like this:
1
2
3
4
5
6
7
8
9
is_palindrome(string str)
{
    if (str is 0 or 1 character) {
         return true;
    } else {
        // strip off the first and last characters of str
        return is_palindrome(stripped string);
    }
}

Work on that until it properly tells you if the input is a palindrome. Don't worry about palindrome_addition() or palindrome_fix_location() until is_palindrome is working.

Original string: lappal
Processed string:  lappal
¿don't you see the extra space there? ¿can you guess where it come from?

1
2
3
4
    if(newstr[start++] != newstr[--end])
        return false;

    return is_palindrome(newstr);
¿what do you think you are doing there? ¿what characters are you comparing? ¿why the recursive call does not reduce the problem?

write pseudocode or a diagram flow.
so your saying to change it to if(newstr[start++] != newstr[end--])

and make my recursive like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
bool is_palindrome(string edited)
{
    int start = 0;
    int end = edited.size() - 1;
    
    if(start >= end)
        return true;
    if(edited[start++] != edited[end--])
        return false;

    return is_palindrome(edited);
    
}


then fix my process so it takes out the space like this?

1
2
3
4
5
6
7
8
9
10
11
string process(string edited)
{
    string newstr = "";
    	for (unsigned int i = 0 ; i < edited.size() - 1  ; i++)
	        if (isalnum(edited[i]))
	        {
	            newstr += tolower(edited[i]);
	        }
	  return newstr;

}

Last edited on
> so your saying to change it to
no, I asked you some questions and you answered none of them.

doing a deskt test
1
2
3
4
5
6
7
8
9
10
11
12
13
// edited = "aaaa"
bool is_palindrome(string edited)
{
    int start = 0;
    int end = edited.size() - 1; //3
    
    if(start >= end)
        return true;
    if(edited[start++] != edited[end--]) //if edited[0] != edited[3], that is if 'a' != 'a'
        return false;

    return is_palindrome(edited); //is_palindrome("aaaa"), no reduction ¿how do you expect to reach the base case?
}
ne555,

between your comments and doing some cout here and there i finally fixed my recursion, could you help me with getting the right position count and listed the final word as a palindrome?

my ouput now is


Original string: lappal
Processed string: lappal
Line is a palindrome

Original string: lapal
Processed string: lapal
Line is a palindrome

Original string: A man, a plan, a canal, Panama!
Processed string: amanaplanacanalpanama
Line is a palindrome

Original string: lap
Processed string: lap
Line is NOT a palindrome
Characters to insert at location 0 are p
Final line: plap

Original string: alapa
Processed string: alapa
Line is NOT a palindrome
Characters to insert at location 1 are p
Final line: palapa

Original string: bath
Processed string: bath
Line is NOT a palindrome
Characters to insert at location 1 are ht
Final line: htbath

Original string: aabcaa
Processed string: aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are c
Final line: caabcaa

Original string: abc...123...cba
Processed string: abc123cba
Line is NOT a palindrome
Characters to insert at location 3 are 3
Final line: 3abc123cba


correct and needed ouput is


Original line: lappal
Processed line: lappal
Line is a palindrome

Original line: lapal
Processed line: lapal
Line is a palindrome

Original line: A man, a plan, a canal, Panama!
Processed line: amanaplanacanalpanama
Line is a palindrome

Original line: lap
Processed line: lap
Line is NOT a palindrome
Characters to insert at location 0 are pa
Final line: palap

Original line: alapa
Processed line: alapa
Line is NOT a palindrome
Characters to insert at location 1 are pa
Final line: apalapa

Original line: bath
Processed line: bath
Line is NOT a palindrome
Characters to insert at location 0 are hta
Final line: htabath

Original line: aabcaa
Processed line: aabcaa
Line is NOT a palindrome
Characters to insert at location 2 are c
Final line: aacbcaa

Original line: abc...123...cba
Processed line: abc123cba
Line is NOT a palindrome
Characters to insert at location 3 are 32
Final line: abc32123cba


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

using namespace std;

string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);

int main()
{
    string line;
    
    while (getline (cin, line) )
    {
    	cout<<"Original string: " << line<< endl;
    	cout<<"Processed string: " << process(line)<< endl;
    	
       if ( is_palindrome(process(line)) )
       {
           cout<<"Line is a palindrome"<< endl;

        }
       else 
        {
           cout<<"Line is NOT a palindrome"<< endl;
           cout<<"Characters to insert at location "<<palindrome_fix_location(process(line))
               <<" are "<<palindrome_addition( process(line), palindrome_fix_location(process(line)))
               <<endl;
           cout<<"Final line: "<<palindrome_addition( process(line), palindrome_fix_location(process(line)))
               + process(line)<<endl;
        }
        
        cout<<endl;    
    }
    
    return 0;
}

/*
    returns a string such that it is in lower case and contains no
    punctuation or spacing
*/

string process(string edited)
{
    string newstr = "";
    	for (unsigned int i = 0 ; i <= edited.size() - 1  ; i++)
	        if (isalnum(edited[i]))
	        {
	            newstr += tolower(edited[i]);
	        }
	  return newstr;

}


bool is_palindrome(string edited)
{

    if (edited.size() == 0 || edited.size() == 1)
        return true;
    else 
    {
        if (edited[0] != edited[edited.size() - 1])
             return false;
        else
            return (is_palindrome(edited.substr(1, edited.size() - 2)));
    }


}


int palindrome_fix_location(string edited)
{
    int count = 0;
    
        for(unsigned int i = 0, j = edited.size() - 1; i < (edited.size() / 2); i++, j--)
            if (edited[i] != edited[j])
                count += i;
            
    return count;
    /*
        should be as described below:
○ This function should return a location where some number of characters could be added
to a non-palindrome string to make it a palindrome.
○ There are many ways to do this, for example the string aabcaa can be made into a
palindrome by simply reversing and duplicating the string at position 0 (before the string)
or position 6 (after the string), but a more elegant solution would determine that you could
just add the character c at location 2.
○ An optimal solution will determine the minimum number of characters to add to the string.
You do not need to find the optimal solution but if you do then you will receive 1 extra
credit PD point. Likewise, PD deductions will result if your program always reports that it
should simply reverse the entire string (or the entire string minus one character) and put it
at the beginning or end.
    */
}

string palindrome_addition(string edited, int n)
{
    string letter;
        for(unsigned int i = 0, j = edited.size() - 1; i < (edited.size() / 2); i++, j--)
            if (edited[i] != edited[j])
                 letter += edited[j];
            
            
        return letter;
    /*
 given a string and the location where a
 string should be added to make it a palindrome, this should return the text that needs to be added
 at that location
    */
}
What is your algorithm for palindrome_fix_location? Can you describe in words what you're trying to do? Give a detailed example of how it's supposed to work. Doing this will force you to be clear about what your trying to accomplish and you may find problems with your algorithm. Also, if you can't describe how the algorithm is supposed to work then there's no way you'll be able to implement it. In addition, it will help us determine if your code implements your algorithm.

Looking at your output, I see that your code thinks that to fix "bath" it should insert something at position 1. There's no way to create a palindrome by inserting at position 1, so palindrome_fix_location() must be wrong.
The Main issue was a two part, my Final line output was only concatanating the strings, hence the incorrect palindrome. now i added an insert function but its still off??


the updated possible fix for my palindrome_fix_location might still be off?????

the thought process was that this loop would cycle the begining and compare it to the end then only copy the position number of i on where a possible fix could be added?

1
2
3
4
5
6
7
8
9
int palindrome_fix_location(string edited)
{
    int count = 0;
    
        for(unsigned int i = 0, j = edited.size() - 1; i < (edited.size() / 2); i++, j--)
            if (edited[i] != edited[j - i])
                count += (i);
            
    return count;




Original string: lappal
Processed string: lappal
Line is a palindrome

Original string: lapal
Processed string: lapal
Line is a palindrome

Original string: A man, a plan, a canal, Panama!
Processed string: amanaplanacanalpanama
Line is a palindrome

Original string: lap
Processed string: lap
Line is NOT a palindrome
Characters to insert at location 0 are p
Final line: plap

Original string: alapa
Processed string: alapa
Line is NOT a palindrome
Characters to insert at location 1 are p
Final line: aplapa

Original string: bath
Processed string: bath
Line is NOT a palindrome
Characters to insert at location 0 are ht
Final line: htbath

Original string: aabcaa
Processed string: aabcaa
Line is NOT a palindrome
Characters to insert at location 3 are c
Final line: aabccaa

Original string: abc...123...cba
Processed string: abc123cba
Line is NOT a palindrome
Characters to insert at location 6 are 3
Final line: abc1233cba



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

using namespace std;

string process(string);
bool is_palindrome(string);
int palindrome_fix_location(string);
string palindrome_addition(string, int);

int main()
{
    string line;
    
    while (getline (cin, line) )
    {
    	cout<<"Original string: " << line<< endl;
    	cout<<"Processed string: " << process(line)<< endl;
    	//cout<<"size of process string " << process(line).size();
    	
       if ( is_palindrome(process(line)) )
       {
           cout<<"Line is a palindrome"<< endl;

        }
       else 
        {
           cout<<"Line is NOT a palindrome"<< endl;
           cout<<"Characters to insert at location "
               <<palindrome_fix_location(process(line))
               <<" are "<<palindrome_addition( process(line), palindrome_fix_location(process(line) ) )
               <<endl;
           cout<<"Final line: "<<process(line).insert(palindrome_fix_location(process(line)),
                palindrome_addition( process(line), palindrome_fix_location(process(line)) ) )
               <<endl;
        }
        
        cout<<endl;    
    }
    
    return 0;
}

/*
    returns a string such that it is in lower case and contains no
    punctuation or spacing
*/

string process(string edited)
{
    string newstr = "";
    	for (unsigned int i = 0 ; i <= edited.size() - 1  ; i++)
	        if (isalnum(edited[i]))
	        {
	            newstr += tolower(edited[i]);
	        }
	  return newstr;

}


bool is_palindrome(string edited)
{

    if (edited.size() == 0 || edited.size() == 1)
        return true;
    else 
    {
        if (edited[0] != edited[edited.size() - 1])
             return false;
        else
            return (is_palindrome(edited.substr(1, edited.size() - 2)));
    }


}


int palindrome_fix_location(string edited)
{
    int count = 0;
    
        for(unsigned int i = 0, j = edited.size() - 1; i < (edited.size() / 2); i++, j--)
            if (edited[i] != edited[j - i])
                count += (i);
            
    return count;
}

string palindrome_addition(string edited, int n)
{
    string letter;
        for(unsigned int i = 0, j = edited.size() - 1; i < (edited.size() / 2); i++, j--)
        {
            if (edited[i] != edited[j])
                 letter += edited[j];
        }    

        return letter;
        

    /*
 given a string and the location where a
 string should be added to make it a palindrome, this should return the text that needs to be added
 at that location
    */
}

Last edited on
Topic archived. No new replies allowed.