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

In C++ using a single dimensional array Create a program that uses a for loop to...
In C++ using a single dimensional array Create a program that uses a for loop to input the day, the high temperature, and low temperature for each day of the week. The day, high, and low will be placed into three elements of the array. For each loop the day, high, and low will be placed into the next set of elements of the array. After the days and temps for all seven days have been entered into the array,...
Step by step in python Write a program that will keep asking for a user input...
Step by step in python Write a program that will keep asking for a user input (until a blank line is entered) and will inform me whether what I entered was a valid number or not (without crashing). The program should use at least one try/except loop The program should include at least two custom written functions (a main() function can count as one of the two)
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...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
First, write a program to loop asking for a number from the user until the user...
First, write a program to loop asking for a number from the user until the user inputs a zero or until the user has input 50 numbers. Store each value in an array except the values that are divisible by 5 and display all stored values. Determine how many of the values stored in the array were divisible by 10 and display result. Next, write a function getMinMax that accepts an array of floats and the size of the array,...
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 ...
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate...
Microsoft Visual C++ Assembly language Problem 3. Write a program that uses a loop to calculate the first seven values of the Fibonacci number sequence, described by the following formula: Fib(1) = 1, Fib(2) = 2, … Fib(n) = Fib(n-1) + Fib(n-2). Place each value in the EAX register and display it with a call DumpRegs statement inside the loop For example: Fib(1) = 1, Fib(2) = 2, Fib(3) = Fib(1) + Fib(2) = 3, Fib(4) = Fib(2) + Fib(3)...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT