Question: You have to write a function void checkPalindrome(char filename[], int palindrome[], int &n) to check for palindrome string in all the sentences stored in a

You have to write a function void checkPalindrome(char filename[], int palindrome[], int &n) to check for palindrome string in all the sentences stored in a textfile (sentences.txt). Store the sentence number in array palindrome and n is the number of palindrome strings in textfile. You cannot change the code in main() as it will provide the output as shown in Figure 1.

TEXT FILE

We all agreed; it was a magnificent evening. Eva, Can I Stab Bats In A Cave? A Man, A Plan, A Canal-Panama! Mr. Owl Ate My Metal Worm Of all the places to travel, Mexico is at the top of my list. A Santa Lived As a Devil At NASA Was It A Rat I Saw? There is so much to understand. Do Geese See God? Once you know all the elements, it's not difficult to pull together a sentence. Never Odd Or Even Did you know that, along with gorgeous architecture, it's home to the largest tamale Ere hypocrisies or poses are in, my hymn I erase. So prose I, sir, copy here. 

The following steps shows how to check for palindrome string in function checkPalindrome:

  1. Open the textfile and check if its successfully open.
  2. Create a stack and read every sentence in textfile using getline.
  3. Go through every character in the sentence read and check if its alphabet using isalpha(). If its an alphabet, push the character to stack. Before pushing to stack, convert the character to lower case first using tolower(). Also store this lower case character to a new c-string str. If the sentence read from file is Madam In Eden, I'm Adam then after this step, the stack will contain madaminedenimadam, where every node store one character from the string. The c-string str also contains madaminedenimadam. Remember to append \0 to str.
  4. Pop each character from the stack until the stack is empty. Compare each pop character in stack with one character in str starting from first character. If it is the same then pop next item. However, if its not the same then this is not a palindrome and no need to check the rest of the character store in Stack. Go to next string in the file and check if it is palindrome using the same method.
  5. If after popped all items from the stack and all comparison between the popped char and str are the same, then it is a palindrome. Copy the sentence number to array palindrome. Palindrome array will contain the sentences number as shown in Figure 2.

List.cpp

#include #include "List.h"

using namespace std;

List::List() { head = NULL; count = 0; }

bool List::empty() { if (count == 0) return true; return false; }

int List::size() { return count; }

Node *List::find(int position) { Node *cur;

if (position > count) return NULL; cur = head; for (int count = 1; countnext; return cur; }

bool List::get(int position, type &result) { if (position > count) return false; result = find(position)->item; return true; }

bool List::set(int position, type newItem) { if (position > count) return false; find(position)->item = newItem; return true; }

bool List::insert(int at, type newItem) {// Any simplification can be done on code below? Node *pre, *cur, *tmp = new Node(newItem);

if (at count + 1) return false; if (!tmp) return false;

if (empty()) { head = tmp; count++; return true; } if (at == 1) { tmp->next = head; head = tmp; count++; return true; } pre = find(at - 1); cur = pre->next; tmp->next = cur; pre->next = tmp; count++; return true; }

bool List::remove(int from) { Node *pre, *cur;

if (from count) return false; if (from == 1) { cur = head; head = head->next; count--; free(cur); return true; } pre = find(from - 1); cur = pre->next; pre->next = cur->next; free(cur); count--; return true; }

//this function only used for primitive type only bool List::insert(type newItem) { Node *pre, *cur, *tmp;

tmp = new Node(newItem);

if (!tmp) return false; if (empty()) { head = tmp; count++; return true; } count++; if (head->item >= newItem) { tmp->next = head; head = tmp; return true; } pre = head; cur = pre->next; for (; cur != NULL;) { if (cur->item >= newItem) break; pre = cur; cur = cur->next; } tmp->next = cur; pre->next = tmp; return true; }

void List::print() { Node *current = head;

while (current != NULL) { cout item; current = current->next; }

}

List.h

#ifndef List_type #define List_type

#include "Node.h" //typedef char type;

//using type = char;

struct List { int count; Node *head; Node *find(int); List(); bool empty(); int size(); bool get(int, type&); bool set(int, type); bool insert(int, type); // insert item according to the specified position bool remove(int); bool insert(type); // insert item into ascending order - work for primitve type only void print(); };

#endif

Node.cpp

#include #include "Node.h"

Node::Node(type newItem) { item = newItem; next = NULL; } Node.h

#ifndef Node_type #define Node_type

typedef char type;

//using type = char;

struct Node { public: type item; Node *next; Node(type); };

#endif

Stack.cpp

#include #include "Stack.h"

bool Stack::empty() { return list.empty(); }

int Stack::size() { return list.size(); }

bool Stack::push(type newItem) { return list.insert(1, newItem); }

bool Stack::pop(type &container) { if (!list.get(1, container)) return false; return list.remove(1); }

void Stack::print() { list.print(); }

Stack.h

#ifndef Stack_type #define Stack_type

#include "List.h"

//typedef char type;

//using type = char;

struct Stack { List list; bool empty(); int size(); bool push(type); // insert item bool pop(type&); // remove item void print(); };

#endif

You have to write a function void checkPalindrome(char filename[], int palindrome[], int

DAuccd1024\may2018 assg-test\Debuglassg-test.exe Sentences 2, 3, 4, 6, 7, 9, 11, and 13 are palindrome strings in the text file Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!