I need help solving problem in C++ (Window and Linux -- Vim). I really have no i
ID: 2247516 • Letter: I
Question
I need help solving problem in C++ (Window and Linux -- Vim).
I really have no idea without using string header file, cstring header file or C++ strings in String class.
Please help me out. Thank you.
======================================================================
StString.h (Can be modified)
#ifndef STATIC_STRING_H
#define STATIC_STRING_H
/**
* StaticString class which stores strings
* using fixed-size(static) char arrays.
*
* The char array should be null-terminated so
* as to be compatible with c_string functions.
*
* Certain methods assume an 8-bit ascii char representation.
*
* You are not allowed to remove or rename any of the
* data memebers of methods from this class. However
* you can add additional data members and methods as
* you see fit. You can also change the method
* implementations as needed.
*
*/
class StaticString{
public:
/**
* Default constructor. Initializes an empty string
* with the default buffer capacity.
* @see DEFAULT_CAPACITY
*/
StaticString();
/**
* c-string constructor. Creates a copy of
* the provided c-string. If the string does
* not fit in the buffer an out_of_range error
* should be thrown.
* @param str the c-string to store.
* @throws out_of_range error if the string does not fit in the buffer
*/
StaticString(const char* str);
/** Get the number of chars in the string */
int len() const;
/** Get the total number of chars that can be stored */
int capacity() const;
/** Get a pointer to the char buffer */
const char* c_str() const;
/** Get a reference to the character at the specified position.
* @param position the 0-based index to retreive.
* @return the character at the specified position.
*/
char& char_at(int position);
/** Get a copy of the character at the specified position.
* @param position the 0-based index to retreive.
* @return the character at the specified position.
*/
char char_at(int position) const;
/** Get a reference to the character at the specified position.
* @param position the 0-based index to retreive.
* @return the character at the specified position.
*/
char& operator[](int position);
/** Get a copy of the character at the specified position.
* @param position the 0-based index to retreive.
* @return the character at the specified position.
*/
char operator[](int position) const;
/** Returns true if other is a prefix of this string.
* @param other the string to check as a prefix.
* @return bool true if other is a prefix.
*/
bool isPrefix(const StaticString& other) const;
/** Returns true if other is a prefix of this string
* ignoring character case.
* @param other the string to check as a prefix.
* @return bool true if other is a prefix.
*/
bool isIPrefix(const StaticString& other) const;
/** Returns true if other is a suffix of this string.
* @param other the string to check as a suffix.
* @return bool true if other is a suffix.
*/
bool isSuffix(const StaticString& other) const;
/** Returns true if other is a suffix of this string
* ignoring character case.
* @param other the string to check as a suffix.
* @return bool true if other is a suffix.
*/
bool isISuffix(const StaticString& other) const;
/** Removes leading and trailing whitespace.
* The string is modified "in-place".
* @return the StaticString with all leading and
* trailing whitespace characters removed.
*/
StaticString& trim();
/** Converts all characters to lowercase.
* The characters are modified "in-place".
* @return lowercase StaticString
*/
StaticString& toLower();
/** Converts all characters to uppercase.
* The characters are modified "in-place".
* @return uppercase StaticString
*/
StaticString& toUpper();
/** Returns a concatenation of this string and other.
* The original strings are not modified.
* @param other the string to append.
* @returns the concatenated StaticString
* @throws out_of_range error if the string does not fit in the buffer.
*/
StaticString operator+(const StaticString& other) const;
/** Returns a concatenation of this string and other.
* The original strings are not modified.
* @param other the string to append.
* @returns the concatenated StaticString
* @throws out_of_range error if the string does not fit in the buffer.
*/
StaticString concat(const StaticString& other) const;
private:
static const int CAPACITY=63;
/** A pointer to the underlying null terminated char array */
char cstr[CAPACITY+1]; //Include a spot for the null character
};
#endif
=======================================================================================
StString.cpp (Can be modified)
#include "StString.h"
#include <cstddef>
StaticString::StaticString(){
//TODO:Implement me
}
StaticString::StaticString(const char* str){
//TODO:Implement me
}
int StaticString::len() const{
//TODO:Implement me
return -1;
}
int StaticString::capacity() const{
//TODO:Implement me
return -1;
}
const char* StaticString::c_str() const{
//TODO:Implement me
return nullptr;
}
char& StaticString::char_at(int position){
//TODO:Implement me
char* a = new char('a');
return *a;
}
char StaticString::char_at(int position) const{
//TODO:Implement me
return 'x';
}
char& StaticString::operator[](int position){
//TODO:Implement me
char* a = new char('a');
return *a;
}
char StaticString::operator[](int position) const{
//TODO:Implement me
return 'x';
}
bool StaticString::isPrefix(const StaticString& other) const{
//TODO:Implement me
return false;
}
bool StaticString::isIPrefix(const StaticString& other) const{
//TODO:Implement me
return false;
}
bool StaticString::isSuffix(const StaticString& other) const{
//TODO:Implement me
return false;
}
bool StaticString::isISuffix(const StaticString& other) const{
//TODO:Implement me
return false;
}
StaticString& StaticString::trim(){
//TODO:Implement me
return *this;
}
StaticString& StaticString::toLower(){
//TODO:Implement me
return *this;
}
StaticString& StaticString::toUpper(){
//TODO:Implement me
return *this;
}
StaticString StaticString::operator+(const StaticString& other) const{
//TODO:Implement me
StaticString a;
return a;
}
StaticString StaticString::concat(const StaticString& other) const{
//TODO:Implement me
StaticString a;
return a;
}
=============================================================================
Driver.cpp (DO NOT modify)
#include "StString.h"
#include <iostream>
#include <stdexcept>
#include <cstring>
using std::cout;
using std::endl;
using std::out_of_range;
using std::strcmp;
template <typename T>
void test(int testNum, int& correct, T actual, T expected){
if(actual == expected){
correct++;
cout << "Passed Test " << testNum << endl;
}
else{
cout << "***Failed Test " << testNum << "***" << endl;
cout << "Actual: " << actual << " Expected: " << expected << endl;
}
}
void testString(int testNum, int& correct, const char* actual, const char* expected){
if(actual && expected && strcmp(actual, expected) == 0){
correct++;
cout << "Passed Test " << testNum << endl;
}
else{
cout << "***Failed Test " << testNum << "***" << endl;
if(actual && expected){
cout << "Actual: " << actual << " Expected: " << expected << endl;
}
}
}
int main(){
int testNum = 1;
int correct = 0;
/*Basic initialization and accessor checks*/
/*Checks len, capacity, c_str, char_at, []*/
cout << "--------Accessor Tests--------" << endl;
/*Empty String*/
StaticString s1;
test(testNum++, correct, s1.len(), 0);
test(testNum++, correct, s1.capacity(), 63);
testString(testNum++, correct, s1.c_str(), "");
/*Simple string*/
StaticString s2("abc");
test(testNum++, correct, s2.len(), 3);
test(testNum++, correct, s2.capacity(), 63);
test(testNum++, correct, s2.char_at(0), 'a');
test(testNum++, correct, s2[2], 'c');
testString(testNum++, correct, s2.c_str(), "abc");
/*Long string*/
StaticString s3("1234567890qwertyuiopasdfghjklzxcvbnm");
test(testNum++, correct, s3.len(), 36);
test(testNum++, correct, s3.char_at(3), '4');
/*Symbols*/
StaticString s4("abc ;2420* Rocks!ABC");
test(testNum++, correct, s4.len(), 20);
test(testNum++, correct, s4.char_at(3), ' ');
testString(testNum++, correct, s4.c_str(), "abc ;2420* Rocks!ABC");
/*Max string*/
StaticString s5("1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");
test(testNum++, correct, s5.len(), 63);
test(testNum++, correct, s5.capacity(), 63);
test(testNum++, correct, s5[62], 'c');
testString(testNum++, correct, s5.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");
cout << "--------Comparison Tests--------" << endl;
cout << "--------Prefix--------" << endl;
/*Prefix*/
test(testNum++, correct, s1.isPrefix(s2), false);
test(testNum++, correct, s2.isPrefix(s1), true);
test(testNum++, correct, s2.isPrefix(s2), true);
test(testNum++, correct, s4.isPrefix(s2), true);
test(testNum++, correct, s2.isPrefix(s4), false);
test(testNum++, correct, s5.isPrefix(s3), true);
test(testNum++, correct, s3.isPrefix(s5), false);
test(testNum++, correct, s5.isPrefix(s5), true);
//test(testNum++, correct, s3.isPrefix("1234"), true);
/*IPrefix*/
cout << "--------IPrefix--------" << endl;
test(testNum++, correct, s1.isIPrefix(s2), false);
test(testNum++, correct, s2.isIPrefix(s1), true);
//test(testNum++, correct, s2.isIPrefix("AB"), true);
//test(testNum++, correct, s2.isIPrefix("ABC"), true);
test(testNum++, correct, s2.isIPrefix(s4), false);
//test(testNum++, correct, s4.isIPrefix("ABc ;2"), true);
//test(testNum++, correct, s5.isIPrefix("1234567890qweRTYuiopz"), false);
/*Suffix*/
cout << "--------Suffix--------" << endl;
test(testNum++, correct, s1.isSuffix(s2), false);
test(testNum++, correct, s2.isSuffix(s1), true);
test(testNum++, correct, s2.isSuffix(s2), true);
test(testNum++, correct, s5.isSuffix(s2), true);
test(testNum++, correct, s2.isSuffix(s5), false);
test(testNum++, correct, s5.isSuffix(s5), true);
//test(testNum++, correct, s2.isSuffix("dbc"), false);
//test(testNum++, correct, s3.isSuffix("zxcvbnm"), true);
//test(testNum++, correct, s4.isSuffix(" "), false);
/*ISuffix*/
cout << "--------ISuffix--------" << endl;
test(testNum++, correct, s1.isISuffix(s2), false);
test(testNum++, correct, s2.isISuffix(s1), true);
//test(testNum++, correct, s2.isISuffix("aBC"), true);
//test(testNum++, correct, s4.isISuffix("s!aBc"), true);
//test(testNum++, correct, s2.isISuffix("s?AbC"), false);
cout << "--------Modification Tests--------" << endl;
cout << "--------Concatenation--------" << endl;
/*Concatenation*/
StaticString s6 = s1+s2;
test(testNum++, correct, s6.len(), 3);
testString(testNum++, correct, s6.c_str(), "abc");
StaticString s7 = s2+"abc";
test(testNum++, correct, s7.len(), 6);
testString(testNum++, correct, s7.c_str(), "abcabc");
StaticString s8 = s5+s1;
test(testNum++, correct, s8.len(), 63);
testString(testNum++, correct, s8.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREabc");
StaticString s9 = s3.concat(s4);
test(testNum++, correct, s9.len(), 56);
testString(testNum++, correct, s9.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmabc ;2420* Rocks!ABC");
StaticString s10 = s2.concat("def");
test(testNum++, correct, s10.len(), 6);
testString(testNum++, correct, s10.c_str(), "abcdef");
cout << "--------ToUpper--------" << endl;
/*ToUpper*/
StaticString s11 = s1;
s11.toUpper();
test(testNum++, correct, s11.len(), 0);
testString(testNum++, correct, s11.c_str(), "");
StaticString s12 = s2;
s12.toUpper();
test(testNum++, correct, s12.len(), 3);
testString(testNum++, correct, s12.c_str(), "ABC");
StaticString s13 = s4;
s13.toUpper();
testString(testNum++, correct, s13.c_str(), "ABC ;2420* ROCKS!ABC");
StaticString s14 = s5;
s14.toUpper();
testString(testNum++, correct, s14.c_str(), "1234567890QWERTYUIOPASDFGHJKLZXCVBNMMNBVCXZLKJHGFDSAPOIUYTREABC");
/*ToLower*/
cout << "--------ToLower--------" << endl;
StaticString s15 = s1;
s15.toLower();
test(testNum++, correct, s15.len(), 0);
testString(testNum++, correct, s15.c_str(), "");
StaticString s16 = s4;
s16.toLower();
testString(testNum++, correct, s16.c_str(), "abc ;2420* rocks!abc");
StaticString s17 = s5;
s17.toLower();
testString(testNum++, correct, s17.c_str(), "1234567890qwertyuiopasdfghjklzxcvbnmmnbvcxzlkjhgfdsapoiuytreabc");
/*Trim*/
StaticString s19 = "";
s19.trim();
test(testNum++, correct, s19.len(), 0);
testString(testNum++, correct, s19.c_str(), "");
StaticString s20 = " abc ";
s20.trim();
test(testNum++, correct, s20.len(), 3);
testString(testNum++, correct, s20.c_str(), "abc");
StaticString s21 = " !a b $ ";
s21.trim();
test(testNum++, correct, s21.len(), 6);
testString(testNum++, correct, s21.c_str(), "!a b $");
StaticString s23 = " ";
s23.trim();
test(testNum++, correct, s23.len(), 0);
testString(testNum++, correct, s23.c_str(), "");
/*Assignment*/
s2[0] = 'b';
s3[3] = 'a';
s4[1] = 'q';
s5.char_at(8) = 'z';
s6.char_at(0) = ' ';
test(testNum++, correct, s2[0], 'b');
test(testNum++, correct, s3.char_at(3), 'a');
test(testNum++, correct, s4.char_at(1), 'q');
test(testNum++, correct, s5[8], 'z');
test(testNum++, correct, s6[0], ' ');
/*Bounds checking*/
try{
s1.char_at(1);//out_of_bounds
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
try{
s2.char_at(-1);//out_of_bounds
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
try{
s1[0];//out_of_bounds
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
try{
s3+s3;//too large
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
try{
s8+"1111122222333334444455555666";//too large
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
try{
StaticString s29 = "..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... .....";
test(testNum++, correct, 0, 1);
}
catch(out_of_range){
test(testNum++, correct, 0, 0);
}
cout << "Passed " << correct << "/" << --testNum << " tests" << endl;
cout << "Score: " << correct/float(testNum) << endl;
cout << "Points: " << 60*correct/float(testNum) << endl;
}
Explanation / Answer
Given below is the completed implementation of the functions in StString.cpp. Please use the below StString.cpp with StString.h and main.cpp provided in the question. All the tests in the main.cpp passed. Output shown in the end. Please don't forget to rate the answer if it helped. Thank you very much.
StString.cpp
#include "StString.h"
#include <stdexcept>
StaticString::StaticString(){
cstr[0] = '';
}
StaticString::StaticString(const char* str){
int size = 0;
//find the lenght of input str
while(str[size] != '')
size++;
if(size > CAPACITY)
throw std::out_of_range("string length is larger");
for(int i = 0; i <= size; i++)
cstr[i] = str[i];
}
int StaticString::len() const{
int size = 0;
while(cstr[size] != '')
size++;
return size;
}
int StaticString::capacity() const{
return CAPACITY;
}
const char* StaticString::c_str() const{
return cstr;
}
char& StaticString::char_at(int position){
if(position < 0 || position >= len())
throw std::out_of_range("position out of range");
return cstr[position];
}
char StaticString::char_at(int position) const{
if(position < 0 || position >= len())
throw std::out_of_range("position out of range");
return cstr[position];
}
char& StaticString::operator[](int position){
if(position < 0 || position >= len())
throw std::out_of_range("position out of range");
return cstr[position];
}
char StaticString::operator[](int position) const{
if(position < 0 || position >= len())
throw std::out_of_range("position out of range");
return cstr[position];
}
bool StaticString::isPrefix(const StaticString& other) const{
int len1 = other.len();
if(len1 <= len()) //only if prefix is smaller than the string
{
for(int i = 0; i < len1; i++)
{
if(cstr[i] != other.cstr[i]) //any corresponding characters not matching
return false;
}
return true;
}
return false;
}
bool StaticString::isIPrefix(const StaticString& other) const{
int len1 = other.len();
char ch1, ch2;
if(len1 <= len()) //only if prefix is smaller than the string
{
for(int i = 0; i < len1; i++)
{
ch1 = cstr[i];
ch2 = other.cstr[i];
//convert ch1 and ch2 if upper case to lower case
if(ch1 >= 'A' && ch1 <= 'Z')
ch1 = ch1 - 'A' + 'a';
if(ch2 >= 'A' && ch2 <= 'Z')
ch2 = ch2 - 'A' + 'a';
if(ch1 != ch2) //characters not matching
return false;
}
return true;
}
return false;
}
bool StaticString::isSuffix(const StaticString& other) const{
int idx1 = len(), idx2 = other.len();
if(idx2 <= idx1) //only if prefix is smaller than the string
{
//comparing from backwards
while(idx2 >= 0)
{
if(cstr[idx1] != other.cstr[idx2]) //any corresponding characters not matching
return false;
idx2--;
idx1--;
}
return true;
}
return false;
}
bool StaticString::isISuffix(const StaticString& other) const{
int idx1 = len(), idx2 = other.len();
int ch1, ch2;
if(idx2 <= idx1) //only if prefix is smaller than the string
{
//comparing from backwards
while(idx2 >= 0)
{
ch1 = cstr[idx1];
ch2 = other.cstr[idx2];
if(ch1 >= 'A' && ch1 <= 'Z')
ch1 = ch1 - 'A' + 'a';
if(ch2 >= 'A' && ch2 <= 'Z')
ch2 = ch2 - 'A' + 'a';
if(ch1 != ch2)
return false;
idx2--;
idx1--;
}
return true;
}
return false;
}
StaticString& StaticString::trim(){
int size = len();
if(size > 0)
{
int idx1 = 0;
//get the index of the 1st non-space character starting from begining
while(cstr[idx1] == ' ' || cstr[idx1] == ' ' || cstr[idx1] == ' ' || cstr[idx1] == ' ')
idx1++;
//get the index of the last non-space character
int idx2 = size - 1;
while(cstr[idx2] == ' ' || cstr[idx2] == ' ' || cstr[idx2] == ' ' || cstr[idx2] == ' ')
idx2--;
//shift all characters between idx1 and idx2 by idx1 characters
if(idx1 != 0 || idx2 != size -1 ) // only leading or trailing spaces were present
{
int k = 0;
for(int i = idx1; i <= idx2; i++, k++)
cstr[k] = cstr[i];
cstr[k] = ''; //put the ending null terminator
}
}
return *this;
}
StaticString& StaticString::toLower(){
int size = len();
char ch;
for(int i = 0; i < size; i++)
{
ch = cstr[i];
if(ch >='A' && ch <= 'Z')
cstr[i] = ch - 'A' + 'a';
}
return *this;
}
StaticString& StaticString::toUpper(){
int size = len();
char ch;
for(int i = 0; i < size; i++)
{
ch = cstr[i];
if(ch >='a' && ch <= 'z')
cstr[i] = ch - 'a' + 'A';
}
return *this;
}
StaticString StaticString::operator+(const StaticString& other) const{
return this->concat(other);
}
StaticString StaticString::concat(const StaticString& other) const{
int len1 = len(), len2 = other.len();
if(len1 + len2 > CAPACITY)
throw std::out_of_range("+ will lead to larger string");
//initialize a new string with the 1st string
StaticString a(cstr);
//copy over the other string contents till null terminator at the end of new string
for(int i = 0, j = len1; i <= len2; i++, j++)
a.cstr[j] = other.cstr[i];
return a;
}
output
--------Accessor Tests--------
Passed Test 1
Passed Test 2
Passed Test 3
Passed Test 4
Passed Test 5
Passed Test 6
Passed Test 7
Passed Test 8
Passed Test 9
Passed Test 10
Passed Test 11
Passed Test 12
Passed Test 13
Passed Test 14
Passed Test 15
Passed Test 16
Passed Test 17
--------Comparison Tests--------
--------Prefix--------
Passed Test 18
Passed Test 19
Passed Test 20
Passed Test 21
Passed Test 22
Passed Test 23
Passed Test 24
Passed Test 25
--------IPrefix--------
Passed Test 26
Passed Test 27
Passed Test 28
--------Suffix--------
Passed Test 29
Passed Test 30
Passed Test 31
Passed Test 32
Passed Test 33
Passed Test 34
--------ISuffix--------
Passed Test 35
Passed Test 36
--------Modification Tests--------
--------Concatenation--------
Passed Test 37
Passed Test 38
Passed Test 39
Passed Test 40
Passed Test 41
Passed Test 42
Passed Test 43
Passed Test 44
Passed Test 45
Passed Test 46
--------ToUpper--------
Passed Test 47
Passed Test 48
Passed Test 49
Passed Test 50
Passed Test 51
Passed Test 52
--------ToLower--------
Passed Test 53
Passed Test 54
Passed Test 55
Passed Test 56
Passed Test 57
Passed Test 58
Passed Test 59
Passed Test 60
Passed Test 61
Passed Test 62
Passed Test 63
Passed Test 64
Passed Test 65
Passed Test 66
Passed Test 67
Passed Test 68
Passed Test 69
Passed Test 70
Passed Test 71
Passed Test 72
Passed Test 73
Passed Test 74
Passed Test 75
Passed 75/75 tests
Score: 1
Points: 60
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.