palindrome help please

i have no idea what i am suppose to do for the output, maybe i am writing the code wrong
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
  #include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;
//-------------------------------------------------------------------------------------------------
void input(string&s);
int isPalindrome(const string &s);
void print(string s, string A[], int z);
//-------------------------------------------------------------------------------------------------
int main(){
char repeat;
do
{
	int count=0;
	string x,A[count];
	int store;
	
	input(x);
	store=isPalindrome(x);
	print(x,A,store);


cout << endl << "Do you wish to continue? Enter w or y.";
cin >> repeat;
}while(repeat == 'w' || repeat == 'y');

return 0;
}
//-------------------------------------------------------------------------------------------------
void input(string&s)
{
	cout << "Enter a word and this will check if it is a palimdrome or not: ";
	cin >> s;
}

int isPalindrome(const string &s)
{
	int x,mid;
	
	x = s.size() - 1; // marks end of string s
	mid = s.size() / 2 + 1; // marks middle of string s
	
	for(int count = 0; count != mid; ++count, --x)
	{
		if (s[count] != s[x]) { return -1; } // palindrome
	}
	
	return 0; // not palindrome
}

void print(string s, string A[], int z)
{
	int count;
	if (z == -1)
		cout << "The string " << s << " is a palindrome." << endl;
	
	else
		cout << "The string " << s << " is not a palindrome." << endl; //we want to the program to look at the first element and the last element at the same time, how do i do that?
		cout << "The character in position " << A[count] <<" is " << s.size() << endl;
		cout << "The character in position " << A[s.size()-1] << " is " <<  A[s.size()] << endl;// what goes in here? do i need to declare another variable to get the 2 mismatched position 
}
Duplicate: http://www.cplusplus.com/forum/beginner/202570/

Anyways, corrected version (compiled and tested):

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

void input(std::string &);
bool isPalindrome(const std::string &);
void print(const std::string &);

int main()
{
    std::string str;
    char repeat;
    
    do
    {
        input(str);
        print(str);
        
        std::cout << "\nDo you want to continue (y or n): " << std::flush;
        while ( !(std::cin >> repeat) ||
               (std::tolower(repeat) != 'y' && std::tolower(repeat) != 'n' ) )
        {
              std::cin.clear();
              std::cin.ignore(1000000, '\n');
              std::cout << "y or n: " << std::flush;     
        }
        std::cout << '\n';
    }
    while (std::tolower(repeat) != 'n');
    
    return 0;
}

void input(std::string &palindrome)
{
	std::cout << "Enter a word and this will check if it is a palindrome or not: ";
	
	std::cin >> palindrome;
}

bool isPalindrome(const std::string &s)
{
	std::string::size_type x = s.size() - 1;
	std::string::size_type mid = s.size() / 2 + 1;
	
	for(std::string::size_type ix = 0; ix != mid; ++ix, --x)
	{
		if (s[ix] != s[x]) { return false; }
	}
	
	return true;
}

void print(const std::string &str)
{
    if (isPalindrome(str))
    {
        std::cout << "\nThe string " << str << " is a palindrome.\n";
    }
    else
    {
        std::cout << "\nThe string " << str << " is not a palindrome.\n";
        std::cout << "The first character is " << str[0] << ".\n";
        std::cout << "The last character is " << str[str.size() - 1] << ".\n";
    }
}


Sample output:

Enter a word and this will check if it is a palindrome or not: pip

The string pip is a palindrome.

Do you want to continue (y or n): y

Enter a word and this will check if it is a palindrome or not: pie

The string pie is not a palindrome.
The first character is p.
The last character is e.

Do you want to continue (y or n): g
y or n: 3
y or n: wywst
y or n: n


Last edited on
Topic archived. No new replies allowed.