In: Computer Science
Write a program using c++.
Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit.
Your program must have (and use) at least four VALUE RETURNING functions. Start out by writing your own reverse, uppercase and filter functions.
Then write a boolean function that uses the above functions to see if the given string is a palindrome.
A palindrome checker function that returns true if the passed string is a palindrome or returns false otherwise.
This is a test 123. → false
Do geese see god? → true
Note, this function should use the previous functions (uppercase, reverse and filter) to do its work
A string is a palindrome if it is the same forwards and backwards (once you uppercase it and filter out the non letters and numbers Finally, write a driver function (either in main or in its own function) that has a loop to keep asking the user for strings to check, calls the palindrome checking function and has a counter to count the sentences and palindromes. When the user types END, the program should print out the # of sentences and # of palindromes and quit.
Here is an example: Enter a string: Do geese see
God?
Do geese see God? is a palindrome
Enter a string: This is just junk
This is just junk is NOT a palindrome
Enter a string: Murder for a jar of red rum.
Murder for a jar of red rum. is a palindrome
Enter a string: Some men interpret nine memos.
Some men interpret nine memos. is a palindrome
Enter a string: Never odd or even.
Never odd or even. is a palindrome
Enter a string: 1111111
1111111 is a palindrome
Enter a string: END
This is what I have so far:
#include <iostream>
using namespace std;
string filter(string s){
string r = ""; // nothing between quotes
//string s = "B Cooper 123 # $ . ! 456";
unsigned int i =0;
while (i < s.length()){
char c = s[i];
if (isalnum(c)) // alphabet or number
r += c;
i++;
}
return r; // BCooper123456
}
//Next is uppercase function
string uppercase(string s){
string r = ""; // nothing between quotes
//string s = "B Cooper 123";
for(unsigned int i =0; i < s.length(); i++){
char c = s[i];
r += toupper(c);
}
return r; // B COOPER 123
}
// Next is the reverse
string reverse(string s){
//string s = "B cooper";
string r = "";
int i =s.length() -1;
while (i >= 0){
r += s[i];
i--;
}
return r;
}
// palindrome checker
bool isPal(string s) {
string fu = filter(uppercase(s));
if ( fu == reverse(fu) )
return true;
else
return false;
}
int main()
{
int palcount = 0;
cout << "Enter a sentence." << endl;
getline(cin,s);
while (s != "END"){
if (isPal(s)){
cout << s << " is a palindrome" << endl;
++palcount;
}
else (isPal(s)){
cout << s << " is NOT a palindrome" <<
endl;
}
cout << "Enter a sentence." << endl;
getline(cin,s);
}
cout << "PalCount: " << palcount << endl;
}
Code:
#include <iostream>
#include<string>
using namespace std;
//filter function that takes string as a parameter
//it return a string of only alphabets and numbers
string filter(string s){
string r = ""; // nothing between quotes
//string s = "B Cooper 123 # $ . ! 456";
unsigned int i =0;
//loop until the i less than string length
while (i < s.length()){
//store character at index i in
c
char c = s[i];
//check if it is alphabet or
number
if (isalnum(c))
//if it is
alphabet or number then append it to r
r += c;
//increment i
i++;
}
//return the string that contains alphabets and
numbers
return r; // BCooper123456
}
//Next is uppercase function
//it takse one parameter string
//converts the all letters to uppercase and return it
string uppercase(string s){
string r = ""; // nothing between quotes
//string s = "B Cooper 123";
//loop until the end of string length
for(unsigned int i =0; i < s.length(); i++){
//store the character at index i in
c
char c = s[i];
//convert it to uppercase.
r += toupper(c);
}
//return the string in upper case
return r; // B COOPER 123
}
// Next is the reverse
//it takes the one parameter string
//return the reversed string
string reverse(string s){
//string s = "B cooper";
string r = "";
//store the length of string in i here i be the index
value so -1
int i =s.length() -1;
while (i >= 0){
//append the last character to the
r
r += s[i];
//decrement i value
i--;
}
//return the reversed string
return r;
}
// palindrome checker
// it takes a string as a parameter
//it return either true or false
bool isPal(string s) {
//call the functions filter and upper
//pass string s as a paramter
string fu = filter(uppercase(s));
//check if the fu string and reverse of fu are
equal
//if they are equal they are palindrome return
ture
if ( fu == reverse(fu))
return true;
//if they are not equal then return false.
else
return false;
}
int main()
{
// declare two variables palcount and
sentence_typed
int palcount = 0,sentence_typed=0;
//declare a string s
string s;
//read the string from user
cout << "Enter a string: ";
//read the string
getline(cin,s);
//increment the sentence_typed value
sentence_typed++;
//check if the string s if not equal to END
while (s != "END"){
// make a function call to isPal pass s as
parameter
// if it is palindrome then we print
palindrome and increment palcount
if (isPal(s)){
cout << s
<< " is a palindrome" << endl<<endl;
++palcount;
}
// print it is not palindrome
else{
cout << s
<< " is NOT a palindrome" << endl<<endl;
}
// ask the user to enter next string
cout << "Enter a string:
";
// read the string
getline(cin,s);
// check if the entered string is not
equal to END
// then increment the sentence_typed
value
if(s!="END"){
sentence_typed++;
}
}
//print the sentence typed and palcount
cout<<endl<<"Sentence Typed :
"<<sentence_typed<<endl<<endl;
cout << "PalCount: " << palcount <<
endl<<endl;
return 0;
}