Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:"<>k; char c=s.at(k); cout<<"The character is:"<>str; int i=str.compare(s);

if(i==0) cout<<"They are equal"<>index; cout<<"Enter no of chars:"; cin>>n; int j=str.compare(index,n,str); if(j==0) cout<<"The characters are equal:"<>index; cout<<"Enter no of chars:"; cin>>n; char st[]={"pqrst"}; A.copy(st,index,n); int f=A.find('c'); if(f==0) cout<<"character not found in the string."<>str; cout<<"Enter the position"<>pos; int f2=s.find(str,pos); if(f2==0) cout<<"character 'a' not found in the string at that position."<

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

1. Which is the most abundant gas presented in the atmosphere?

Answered: 1 week ago

Question

LO3 Define the difference between job satisfaction and engagement.

Answered: 1 week ago