implementation of mutable string in cpp
#include <iostream>
class Mstring {
private:
int terminator;
public:
char seq[100];
Mstring(){
terminator = 0;
seq[terminator] = '\0';
}
//moves the terminator reference up, and re-establishes the end of the char seq;
void addchar(char elem) {
seq[terminator] = elem;
terminator++;
seq[terminator] = '\0';
}
//removes a certain amount of chars from the mstring.
void delchar(int amount) {
for(int i=0;i<amount;i++) {
if(terminator > 0) {
terminator -= 1;
seq[terminator] = '\0';
}
}
}
//prints the current sequence
void printseq() {
std::cout << seq << std::endl;
}
};
int main() {
Mstring test;
test.addchar('i');
test.addchar('y');
test.addchar('f');
test.addchar('c');
test.delchar(2);
test.printseq();
test.addchar('u');
test.addchar('t');
test.printseq();
return 0;
}