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

Write a String class that performs some basic operations. The class String like

ID: 3538752 • Letter: W

Question

Write a String class that performs some basic operations. The class String like the following:
Data:
Each String object should contain the following data as part of its internal representation:
- an array of characters of (sufficient) fixed length to store a string.
- a length field that will hold the current length of the string at any given moment.

Operations:
The String class should provide the following methods for setting up, accessing and changing strings:

String(); // A default constructor that sets up the String to be an empty string.
void assign(const char s[]); // A method that copies the contents of the passed string into the String.
void append(const String &str); //A method that appends the contents of the passed String onto the end of the String the method is called on.
int compare_to(const String &str) const;
//A method that compares the String this method is called on with the String passed to the method. If the string is greater than the string passed, it should return a positive value; if less than, a negative value, and if equal, the value zero.
void print() const; //A method that prints the value of the String it is called on to cout.
int length() const; //A method that returns the length of the String that it is called on.
char element(int i) const;
// A method that returns the i-th element (zero-based, just like arrays) of the String it is called on. This method should print out an error message and return the null character () if the location asked for is out-of-range for the string.

Explanation / Answer

please rate - thanks

any problems I will fix in the morning


#include <iostream>
using namespace std;
class String
{
public:
String(); // A default constructor that sets up the String to be an empty string.
void assign(const char s[]); // A method that copies the contents of the passed string into the String.
void append(const String &str); //A method that appends the contents of the passed String onto the end of the String the method is called on.
int compare_to(const String &str) const;
//A method that compares the String this method is called on with the String passed to the method. If the string is greater than the string passed, it should return a positive value; if less than, a negative value, and if equal, the value zero.
void print() const; //A method that prints the value of the String it is called on to cout.
int length() const; //A method that returns the length of the String that it is called on.
char element(int i) const;
// A method that returns the i-th element (zero-based, just like arrays) of the String it is called on. This method should print out an error message and return the null character () if the location asked for is out-of-range for the string.

private:
        char c[100];
        int characters;
};
String::String()
{characters=0;
c[0]='';
}
void String::assign(const char s[])
{int i=0;
while(s[i]!='')
     {c[i]=s[i];
     i++;
     }
c[i]='';
characters=i;
}
void String::append(const String &str)
{int i=0,j=characters;
while(str.c[i]!='')
    {c[j]=str.c[i] ;
    i++;
    j++;
    }
characters=j;
c[j]='';
}
int String::compare_to(const String &str) const
{int i;
for(i=0;c[i]!=''||str.c[i]!='';i++)
    if(c[i]<str.c[i])
         return -1;
    else if (c[i]>str.c[i])
         return 1;

if(c[i]==''&&str.c[i]=='')
      return 0;
if(c[i]=='')
      return -1;
else
      return 1;
}
         

void String::print() const
{cout<<"String: "<<c<<endl;
}
int String::length() const
{return characters;
}
char String::element(int i) const
{if(i<0||i>=characters)
      {cout<<"index "<<i<<" out of range ";
      return '';

}
return c[i];

};
// A method that returns the i-th element (zero-based, just like arrays) of the String it is called on. This me

int main()
{String s,s1 ;
s.assign("Hello my");
s.print();
cout<<"Which has "<<s.length()<<" characters ";
s1.assign(" friend and neighbor");
s.append(s1);
s.print();
cout<<"Which has "<<s.length()<<" characters ";
cout<<"character 0 of (s)"Hello my" is "<<s.element(0)<<endl;
cout<<"character -5 of (s)"Hello my" is "<<s.element(-5)<<endl;
cout<<"character "<<s.length()<<" of (s)"Hello my" is "<<s.element(s.length())<<endl;
cout<<"character "<<s.length()-1<<" of (s)"Hello my" is "<<s.element(s.length()-1)<<endl;
cout<<s.compare_to(s)<<endl;
cout<<s1.compare_to(s)<<endl;
cout<<s.compare_to(s1)<<endl;
    system("pause");
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote