Question
Introduction to Programming with C++ (3rd Edition) Chapter 11, Problem 14PE please help the program is not compiling //PE11.14 //Implementation of MyString class class MyString
Introduction to Programming with C++ (3rd Edition)
Chapter 11, Problem 14PE
please help
the program is not compiling
//PE11.14 //Implementation of MyString class class MyString { public: //default sets string to NULL MyString(void); //Takes in a string and creates an object MyString(const char* input); //displays length of string excluding null int length();
//clear the inputted string void clear(); // if string is empty returns true else it returns //false char& at(int loc) {bool empty();} //compare one string with another int compare(const MyString &s) ; // compare a string with its index and number of //characters int compare(int index,int n, const MyString &s);
void copy (char s[],int index,int n);
int find(char ch);
int find(char ch,int index);
int find (const MyString& S, int index); private: char* nstring; short nlength; }; //default constructor MyString::MyString() { nlength = 1; nstring = new char[nlength]; nstring[nlength-1] = '\0'; } //takes a string with object created MyString::MyString(const char *input) { nlength = strlen(input)+1 ; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = input[i]; } nstring[(nlength-1)] = '\0'; }
int MyString::length() { int i = 0; while (nstring[i] != '\0') { i++; }
return i; }
void MyString::clear() { std::string str; int i=0; std::cout << "Please enter a string to clear"; cin>>str; while(str[i] != '\0') { str[i]=' ';i++; } } char& MyString::at(int loc) { return nstring[loc-1]; }
bool MyString::empty() { if(nstring[0] == '\0'&& nlength == 1){return true;} else {return false;} }
int MyString::compare(const MyString &str2) { string str1; nlength = str2.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = str2.nstring[i]; } int i=0,flag=0; while (str1[i]!='\0' && nstring[i]!='\0') { if(str1[i]!=nstring[i]){ flag=1; break; } i++; } if (flag==0 && str1[i]=='\0' && nstring[i]=='\0') return 1; else
return 0; }
int MyString::compare(int index,int n,const MyString &str2) { string str="ab"; nlength = str2.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = str2.nstring[i]; } int k=0; for(int j=index; j void MyString::copy(char s[],int index,int n) { char s1[50]; int j=0; for(int i=index;i int MyString::find(char ch) { string str; cout<<"Enter string for finding a character:"; cin>>str; for(int i=0; str[i]!='\0';i++) { if(str[i]==ch) return 1; } return 0; } int MyString::find(char ch, int index) { string str; cout<<"Enter string for finding a character in a particular position:"; cin>>str; if(str[index]==ch) return 1; else return 0; } int MyString::find(const MyString &s, int index) { nlength = s.nlength; nstring = new char[nlength]; for (int i = 0; i < (nlength - 1); i++) { nstring[i] = s.nstring[i]; } if(nstring[index]=='a') return 1; else return 0; } //main function int Prog11_14() { //object created MyString A; string s, str; cout<<"Enter a string:"< if(i==0) cout<<"They are equal"<
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started