Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: Palindrome Write a program that uses a stack object to determine if

ID: 3575269 • Letter: E

Question

Exercise 1: Palindrome

Write a program that uses a stack object to determine if a string is a palindrome (i.e. the string is spelled identically backward and forward). The program should ignore spaces and punctuation. Go ahead and start your program by reading in a C-style string from standard input using the getline function. You may assume a limit of 100 characters on the string. Your algrotihm must make use of a stack (of type char). Use the implementation of Stack from stack.h (don't change the file). Ignore spacing, punctuation, special characters, and digits (i.e. only count letters). Upper and lower case equivalent letters should be counted as the same (e.g. 'B' and 'b' are matching letters). Since you are reading data into a C-style string to begin, you may use any of the libraries , , and . Sample runs (user input is underlined): Please enter a string: > ABCDEFGHGFEDCBA “ABCDEFGHGFEDCBA” IS a palindrome Please enter a string: > The quick brown fox “The quick brown fox” is NOT a palindrome Please enter a string: > Cigar? Toss it in a can. It is so tragic. “Cigar? Toss it in a can. It is so tragic.” IS a palindrome

Exercise 2: List

Modify the List class so that it has two more functions which will allow inserts and removes from anywhere in the linked list. Your functions should be called: • insertMiddle • removeMiddle Your functions should have all the same features as the given insert and remove functions except that yours have one extra parameter. The second parameter on each of your functions should be of type int, representing the position at which to insert/delete. Sample calls for a list of integers: L.insertMiddle(345, 5); //attempts to insert the value 345 as the 5th item in the list L.removeMiddle(x, 10); //attempts to delete the 10th item in the list and captures its value into x. For insertMiddle, if the position number is larger than the number of items in the list, just insert the item at the back. If it's too small (0 or less), insert at the front. For removeMiddle, return false if the position is invalid (without removing anything). You can use menu7.cpp to test these features.

//----- menu7.cpp ------//

//----- list.h ------//

//------- listnode.h ------//

//------ stackcomposition.h -----//

Explanation / Answer

#include <iostream>
#include <stack>
#include <string.h>
using namespace std;

int main(){
   stack<char> s;
   char str[1000];
   cout << "Enter a string: ";
   cin.getline(str, 1000);
   int len = 0;
   for(int i = 0; i < strlen(str); i++){
       if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) len++;
   }
   int count = 0;
   for(int i = 0; i < strlen(str); i++){
       if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')){
           char temp = str[i];
           if(temp >= 'A' && temp <= 'Z')
               temp -= ('A' - 'a');
           if(count == len / 2 && len % 2 != 0) continue;
           if(count < len / 2){
               s.push(temp);
               count++;
           }
           else{
               if(s.top() == temp){
                   s.pop();
               }
               else{
                   cout << str << " is NOT a palindrome. ";
                   return 0;
               }
           }
       }
       else continue;
   }
   cout << str << " is a palindrome ";
}