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

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

ID: 3907063 • Letter: #

Question

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

class Sentence
{   
public:
char* arr;
Sentence(char* a){
arr = (char*)malloc(sizeof(char)*(strlen(a)+1));
strcpy(arr,a);
}
char* get_text(){
return arr;
}
void reverse(){
reverseArray(arr, 0, strlen(arr)-1);
}

void reverseArray(char* arr, int i, int j){
if(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
reverseArray(arr,i+1,j-1);
}
}
};


int main(){
char a[] = "Hello!";
char* arr = a;
Sentence greeting(arr);
greeting.reverse();
cout << greeting.get_text() << " ";return 0;
};

Modify the Object Oriented program you just completed so that your program is able to read several different sentences from a file then make a call using the greeting object so that each sentence will display in a reverse order. Use your own test case sentences to test your program.

Explanation / Answer

#define _CRT_SECURE_NO_DEPRECATE

#include<iostream>

#include<stdlib.h>

#include<string.h>

//include fstream.h for file operations

#include<fstream>

#include<string>

//declare MAX lines in file

#define MAX 200

using namespace std;

class Sentence

{

public:

char* arr;

Sentence(char* a) {

arr = (char*)malloc(sizeof(char)*(strlen(a) + 1));

strcpy(arr, a);

}

char* get_text() {

return arr;

}

void reverse() {

reverseArray(arr, 0, strlen(arr) - 1);

}

void reverseArray(char* arr, int i, int j) {

if (i<j) {

char temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

reverseArray(arr, i + 1, j - 1);

}

}

};

int main()

{

//commenting the old code ,CheggEA

/*char a[] = "Hello!";

char* arr = a;

Sentence greeting(arr);

greeting.reverse();

cout << greeting.get_text() << " ";

return 0;*/

//Modyfying program to read from file , CHEGGEA

//declare an array of sentence objects

Sentence **messages;

//allocate memory for the message array

messages = new Sentence*[MAX];

string str;

char *buf;

//declare a variable of type ifstream to read from a file

ifstream in;

in.open("Messages.txt");

if (!in)

{

cout << "Not able to open input file messages.txt" << endl;

return -1;

}

int count = 0;

while (getline(in, str))

{

if (in.fail())

{

cout << "getline error" << endl;

return -1;

}

buf = new char[str.length()+1];

strcpy(buf, str.c_str());

messages[count] = new Sentence(buf);

count++;

}

for (int i = 0; i < count; i++)

{

messages[i]->reverse();

cout << "line " << i + 1 << ": " << messages[i]->get_text() << endl;

}

}

/*

//Messages.txt file

Multithreading is a specialized form of multitasking .

A multitasking is the feature that allows your computer to run two or more programs concurrently.

In general, there are two types of multitasking: process-based and thread-based.

Process-based multitasking handles the concurrent execution of programs.

//output

line 1: . gniksatitlum fo mrof dezilaiceps a si gnidaerhtitluM

line 2: .yltnerrucnoc smargorp erom ro owt nur ot retupmoc ruoy swolla taht erutaef eht si gniksatitlum A

line 3: .desab-daerht dna desab-ssecorp :gniksatitlum fo sepyt owt era ereht ,lareneg nI

line 4: .smargorp fo noitucexe tnerrucnoc eht seldnah gniksatitlum desab-ssecorP

*/