Problem: Write a program to read a string and then determine whether it is a pal
ID: 3644377 • Letter: P
Question
Problem:Write a program to read a string and then determine whether it is a palindrome. Use the following data from a text file to test your program:
input.txt data:
M AD AM
madam
Repaper
463364
57234275
Able was I ere I saw Elba
ABLE WAS I ERE I SAW ELBA
the quick brown fox jumped over the lazy yellow cat
Madam, I'm Adam
A man, a plan, a canal: Panama!
Never odd or even!
Change the characters to all upper case or all lower case using tpupper() or tolower(). Use 3 functions.
The output needs to look like this for each phrase:
**********************************************************************************
Original Str: M ADA M len: 8
Original String: M ADA M len: 8
Reverse String: MADA M len: 8
match: str: M rev_str: M
match: str: A rev_str: A
match: str: D rev_str: D
match: str: A rev_str: A
match: str: M rev_str: M
********** PALINDROME !!!!!!!! *****************
***********************************************************************************
Explanation / Answer
please rate - thanks
if any problems message me before rating-thanks
#include<iostream>
#include <fstream>
#include<string>
using namespace std;
bool isPalindrome(string );
string reverseInput(string);
bool compare(string,string);
int main()
{string input;
bool palin;
ifstream inFile;
inFile.open("input.txt"); //open file
getline(inFile,input);
while(inFile)
{palin=isPalindrome(input);
if(palin)
cout<<"********** PALINDROME !!!!!!!! ***************** ";
else
cout<<"----- Not a Palindrome ----- ";
getline(inFile,input);
}
inFile.close();
system("pause");
return 0;
}
string reverseInput(string input)
{int i,j;
string reverse="";
j=input.length();
for(i=0;i<j;i++)
{reverse=reverse+input[j-i-1];
}
return reverse;
}
bool compare(string input,string reverse)
{int i=-1,j=-1;
bool same=true;
do
{i++;
j++;
while(ispunct(input[i])||input[i]==' ')
i++;
while(ispunct(reverse[j])||reverse[j]==' ')
j++;
if(toupper(reverse[j])==toupper(input[i]))
cout<<"match: ";
else
{cout<<"no match: ";
same=false;
}
cout<<"str: "<<input[i]<<" rev_str: "<<reverse[j]<<endl;
}while(i<input.length()-1);
return same;
}
bool isPalindrome(string input)
{int i=-1;
int k=-1;
int j=input.length();
string reverse;
reverse=reverseInput(input);
cout<<"*************************************************** ";
cout<<"Original Str: "<<input<<" len: "<<input.length()<<endl<<endl;
cout<<"Original String: "<<input<<" len: "<<input.length()<<endl;
cout<<"Reverse String: "<< reverse<<" len: "<<reverse.length()<<endl<<endl;
return compare(input,reverse);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.