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
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
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
using std::cout; using std::endl; using std::out_of_range; using std::strcmp;
template
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("\t"), 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 = " \t!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; }
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