Question

In: Computer Science

Write a program using c++. Write a program that uses a loop to keep asking the...

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;
}

Solutions

Expert Solution

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;
}


Related Solutions

Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Using C++ language, create a program that uses a struct with array variables that will loop...
Using C++ language, create a program that uses a struct with array variables that will loop at least 3 times and get the below information: First Name Last Name Job Title Employee Number Hours Worked Hourly Wage Number of Deductions Claimed Then, determine if the person is entitled to overtime and gross pay. Afterwards, determine the tax and net pay. Output everything to the screen. Use functions wherever possible. Bonus Points: Use an input file to read in an unknown...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
Write a program that uses a for loop to print One of the months of the...
Write a program that uses a for loop to print One of the months of the year is January One of the months of the year is February ...
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a program to reverse each integer number on array (size 3) using while loop. C++...
Write a program to reverse each integer number on array (size 3) using while loop. C++ Input: 3678 2390 1783 Output: 8763 0932 3871
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
Write a c++ program for the Sales Department to keep track of the monthly sales of...
Write a c++ program for the Sales Department to keep track of the monthly sales of its salespersons. The program shall perform the following tasks: Create a base class “Employees” with a protected variable “phone_no” Create a derived class “Sales” from the base class “Employees” with two public variables “emp_no” and “emp_name” Create a second level of derived class “Salesperson” from the derived class “Sales” with two public variables “location” and “monthly_sales” Create a function “employee_details” under the class “Salesperson”...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT