Programming  >  CODING SOLUTION  >  CSCE 121 Introduction to Program Design and Concepts | MyString.cpp (All)

CSCE 121 Introduction to Program Design and Concepts | MyString.cpp

Document Content and Description Below

CSCE 121 Introduction to Program Design and Concepts | MyString.cpp #include "MyString.h" #include <iostream> using std::ostream; using std::cout, std::endl; MyString::MyString(const MyString& s ... tring1) : size1(0), capacity1(1), str(nullptr) { this->size1 = string1.size(); this->capacity1 = string1.capacity(); this->str = new char[this->capacity1]; for (size_t i = 0; i < this->size1; i++) { this->str[i] = string1.str[i]; } this->str[size1] = '\0'; } MyString::MyString() : size1(0), capacity1(1), str(nullptr) { this->size1 = 0; this->capacity1 = 1; this->str = new char[1]; this->str[0] = '\0'; } MyString::MyString(const char* val) : size1(0), capacity1(1), str(nullptr) { this->size1 = 0; this->capacity1 = 1; this->str = nullptr; if (val == nullptr) { this->size1 = 0; this->capacity1 = 1; this->str = new char[this->capacity1]; this->str[0] = '\0'; } else { int i = 0; size_t newsize = 0; while (val[i] != '\0') { newsize += 1; i += 1; } this->size1 = newsize; this->capacity1 = newsize + 1; this->str = new char[this->capacity1]; for (size_t j = 0; j < this->size1; j++) { this->str[j] = val[j]; } this->str[this->size1] = '\0'; } MyString::~MyString() { delete[] str; } void MyString::resize(size_t n) { this->capacity1 = n; char* temp = new char[n]; int index = 0; while (this->str[index] != '\0') { temp[index] = str[index]; index+=1; } temp[index] = '\0'; delete[] this->str; this->str = temp; this->capacity1 = n; } const char& MyString::at (size_t pos) const { if(pos < this->length()) { return this->str[pos]; } else { throw std::out_of_range("Invalid position"); } } size_t MyString::capacity() const { return this->capacity1; } size_t MyString::length() const { return this->size1; } size_t MyString::size() const { return this->size1; } [Show More]

Last updated: 1 year ago

Preview 1 out of 5 pages

Buy Now

Instant download

We Accept:

Payment methods accepted on Scholarfriends (We Accept)
Preview image of CSCE 121 Introduction to Program Design and Concepts  | MyString.cpp document

Buy this document to get the full access instantly

Instant Download Access after purchase

Buy Now

Instant download

We Accept:

Payment methods accepted on Scholarfriends (We Accept)

Reviews( 0 )

$9.00

Buy Now

We Accept:

Payment methods accepted on Scholarfriends (We Accept)

Instant download

Can't find what you want? Try our AI powered Search

155
0

Document information


Connected school, study & course


About the document


Uploaded On

Apr 28, 2023

Number of pages

5

Written in

All

Seller


Profile illustration for PAPERS UNLIMITED™
PAPERS UNLIMITED™

Member since 4 years

509 Documents Sold

Reviews Received
55
20
8
2
8
Additional information

This document has been written for:

Uploaded

Apr 28, 2023

Downloads

 0

Views

 155

More From PAPERS UNLIMITED™

View all PAPERS UNLIMITED™'s documents »

Recommended For You

Get more on CODING SOLUTION »

$9.00
What is Scholarfriends

Scholarfriends.com Online Platform by Browsegrades Inc. 651N South Broad St, Middletown DE. United States.

We are here to help

We're available through e-mail, Twitter, Facebook, and live chat.
 FAQ
 Questions? Leave a message!

Follow us on
 Twitter

Copyright © Scholarfriends · High quality services·