Question
Does any one can show me the code in C++

ll cricket LTE 80% 16:58 Back P2.B MYString v1 Detail Submission Grade For this program, your MYString variables will never n
.111 cricket令 @酉88%- 11:48 Back P2.B MYString v1 Program Detail Submission Grade Member Functions Description return type Def
read one string from the istream argument (could be from cin or an ifstream variable). This should work just like the >>opera
media%2Fe48%2Fe48f1aeb-0061-4420-9ad6-21
@-es 75% l cricket LTE 15:17 Back P2.B MYString v1 SizE Detail Submission Grade of o sort the MYStrings from smallest to larg
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <iomanip>

#include <fstream>

#include <cstring>

#include <cstdlib>

#include <vector>

using namespace std;

// Defines a class MyString

class MyString

{

// Data member to store data

char *str;

int cap;

int end;

public:

// Default constructor to assign default values to data member

MyString()

{

cap = 20;

str = new char[cap];

end = 0;

}// End of default constructor

// Parameterized constructor to assign parameter values to data member

MyString(const char *newStr)

{

cap = strlen(newStr);

str = new char[cap];

end = 0;

}// End of parameterized constructor

// Function to return length

int length()

{

return strlen(str);

}// End of function

// Function to return capacity

int capacity()

{

return cap;

}// End of function

// Function to return character at given index position

char at(int index)

{

return str[index];

}// End of function

// Function to read the file and return true for success

// returns false for not success

bool read(istream &istrm)

{

// Declares a character array with size 100

char arr[100];

// Reads a string from file

if(istrm>>arr)

{

// Copies the string to data member

strcpy(str, arr);

// Increase end counter by one

end++;

return true;

}// End of if condition

// Otherwise returns false

else

return false;

}// End of function

// Function to write the strings to file

void write(ostream &ostrm)

{

// Declares a static variable for number of words to print in a line

static int c = 1;

// If c value is divisible by 6 then

// writes the string with 13 spaces with new line

if(c % 6 == 0)

ostrm<<setw(13)<<str<<endl;

// Otherwise c value is not divisible by 6 then

// writes the string with 13 spaces with out new line

else

ostrm<<setw(13)<<str;

// Increase the counter by one

c++;

}// End of function

// Function to display the strings

void show(MyString arr[], int len)

{

// Loops till length of the array

for(int c = 1; c <= len; c++)

// If c value is divisible by 6 then

// display the string with 13 spaces with new line

if(c % 6 == 0)

cout<<setw(13)<<arr[c-1].str<<endl;

// Otherwise c value is not divisible by 6 then

// display the string with 13 spaces with out new line

else

cout<<setw(13)<<arr[c-1].str;

}// End of function

// Function to sort the strings

void sortMyString(MyString arr[], int len)

{

// Loops till length of the array

for(int c = 0; c < len; c++)

{

// Loops till length of the array minus outer loop variable value minus one

// Length minus out loop variable because after each iteration one record is sored

// minus one because numbers are compared in pair

for(int d = 0; d < len - c - 1; d++)

{

// Checks if the number stored at d index position is greater than

// number stored at next index position

if(strcmp(arr[d].str, arr[d + 1].str) > 0)

{

// Swapping process

MyString temp = arr[d];

arr[d] = arr[d + 1];

arr[d + 1] = temp;

}// End of if condition

}// End of inner for loop

}// End of outer for loop

}// End of function

};// End of class

// main function definition

int main()

{

// Declares an array of MyString of size 100

MyString arr[100];

// Record counter

int c = 0;

// ifstream object declared

ifstream fRead;

// ofstream object declared

ofstream fWrite;

// Opens the file for reading

fRead.open("infile2.txt");

// Opens the file for writing

fWrite.open("outfile.txt", ios::app);

// Checks if the file unable to open for reading display's error message and stop

if(!fRead)

{

cout<<"\n ERROR: Unable to open the file for reading.";

exit(0);

}// End of if condition

// Loops till end of the file

while(!fRead.eof())

{

// Calls the function to read data from file

// and stores it at c index position

// Increase the counter by one

arr[c++].read(fRead);

}// End of while loop

// Calls the function to sort

arr[0].sortMyString(arr, c);

// Checks if the file unable to open for write display's error message and stop

if(!fWrite)

{

cout<<"\n ERROR: Unable to open the file for writing.";

exit(0);

}// End of if condition

// Loops till number of strings

for(int x = 0; x < c; x++)

// Calls the function to write

arr[x].write(fWrite);

// Calls the function to display

arr[0].show(arr, c);

return 0;

}// End of main function

Sample Output:

FATODA2019Uan MyString.exe all whose Press any key to continue. changes of you demo dynamic string for this handle to size Pr

infile2.txt file contents

this
is
a
demo
for
all
of
you
to
handle
dynamic
string
whose
size
changes

outfile.txt file contents

a all changes demo dynamic for
handle is of size string this
to whose you

Add a comment
Know the answer?
Add Answer to:
Does any one can show me the code in C++ ll cricket LTE 80% 16:58 Back...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives You will implement and test a class called MyString. Each MyString object keeps track ...

    Objectives You will implement and test a class called MyString. Each MyString object keeps track of a sequence of characters, similar to the standard C++ string class but with fewer operations. The objectives of this programming assignment are as follows. Ensure that you can write a class that uses dynamic memory to store a sequence whose length is unspecified. (Keep in mind that if you were actually writing a program that needs a string, you would use the C++ standard...

  • IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data...

    IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Have you ever wanted to predict the future? Well the Magic Eight Ball does just that....

    Have you ever wanted to predict the future? Well the Magic Eight Ball does just that. The original game was a softball sized “8-ball”. You would ask a question, shake it up and look at the result. There are 20 responses…10 positive, 5 negative, and 5 are vague. For this project, we want to recreate this, but give the ability to read in a set of responses, and add additional responses, and print out all of the responses in alphabetical...

  • In this lab, you will need to implement the following functions in Text ADT with C++...

    In this lab, you will need to implement the following functions in Text ADT with C++ language(Not C#, Not Java please!): PS: The program I'm using is Visual Studio just to be aware of the format. And I have provided all informations already! Please finish step 1, 2, 3, 4. Code is the correct format of C++ code. a. Constructors and operator = b. Destructor c. Text operations (length, subscript, clear) 1. Implement the aforementioned operations in the Text ADT...

  • Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one me...

    Please try to write the code with Project 1,2 and 3 in mind. And use java language, thank you very much. Create an Edit Menu in your GUI Add a second menu to the GUI called Edit which will have one menu item called Search. Clicking on search should prompt the user using a JOptionPane input dialog to enter a car make. The GUI should then display only cars of that make. You will need to write a second menu...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • Assignment You will be developing a speeding ticket fee calculator. This program will ask for a t...

    Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...

  • Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be...

    Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT