9.8k views
1 vote
create a vector of MYStrings that is size 100 read each of the words from the file called "infile2.txt" (the file is out in Files section under Program Resources). You can call the read function directly on the indexed vector. (do not use vector's push_back function. See programming note below*). Example: while ( words[count].read(fin) ) {...} as you are reading the words in, keep a count of how many words were read in. After you have read in all the words from the file, resize your vector to the correct size based on your count of the number of words read in. sort the MYStrings from smallest to largest (this will be based on the ASCII encoding....meaning capital letters will sort before lower case letters) using Bubble Sort output the sorted words to outfile.txt file 6 words per line ( use setw(13), which is part of library, to space them out....the setw command should not be in the write member function). Below is an example of what you output should look like .....but is missing the middle section.

User Cethint
by
5.7k points

1 Answer

6 votes

Answer:

See Explaination

Step-by-step explanation:

MYString.h

#ifndef MYSTRING_H_INCLUDED

#define MYSTRING_H_INCLUDED

#include <iostream>

using namespace std;

//NDString class

class NDString

{

public:

//member variables

const char * str;

int cap;

int end;

//member functions

NDString();

NDString (const char*);

int length();

int capacity();

char at(int index);

bool read(istream& istr);

void write(ostream& ostr) ;

int compareTo( const NDString& argStr) ;

const char* c_str() ;

void setEqualTo(const NDString& argStr);

};

#endif // MYSTRING_H_INCLUDED

MYString.cpp

#include <iostream>

#include <cstring>

#include "MYString.h"

using namespace std;

//constructor

NDString::NDString()

{

this->str = new char[20];

this->cap = 20;

this->end = 0;

}

//constructor with parameter

NDString::NDString (const char* str)

{

this->str = str;

this->cap=20;

this->end=strlen(str);

}

//return length

int NDString::length()

{

return strlen(str);

}

//return capacity

int NDString::capacity()

{

return cap;

}

//return character at index

char NDString::at(int index)

{

return *(str)+index;

}

//read from file

bool NDString::read(istream& istr)

{

string s;

if(!istr.eof()){

istr>>s;

return true;

}

return false;

}

//write tot file

void NDString::write(ostream& ostr)

{

ostr<<str;

}

//compare two string

int NDString::compareTo( const NDString& argStr)

{

if(this->str==argStr.str)

return 0;

if(strlen(str)<strlen(argStr.str))

return -1;

if(strlen(str)>strlen(argStr.str))

return 1;

}

//return character pointer

const char* NDString::c_str()

{

return this->str;

}

//set equal

void NDString::setEqualTo(const NDString& argStr)

{

this->str=argStr.str;

this->cap=strlen(argStr.str);

}

main.cpp

#include <iostream>

#include "MYString.h"

using namespace std;

//main function

int main()

{

NDString str1, str2("helloall");

NDString str3("1234567890");

cout << str2.c_str() << " "; // str#.c_str()

cout << "\\"<<str2.length() ;

cout << "\\"<<str2.capacity() << endl << endl;

return 0;

}

User AzP
by
6.6k points