Addition of letters might be off by a bit HELP

Hello,

could you assist me in fixing my letters that my palindrome_addition is adding?

i added some logic to look at if the word has an even number of letter or odd and its bringing the incorrect letters still.

output that is incorrect. with words bath and aabcaa


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 pa
Final line: palap

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

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

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

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



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


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
 #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; i < (edited.size() / 2); i++)
        {
            if (edited[i] != edited[edited.size() - 1 - i])
            {
                count += (i);
                break;
            }  
        }

    return count;
}

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

        }    

        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.