#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
usingnamespace 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
}
#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]) { returnfalse; }
}
returntrue;
}
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