I just took an exam where I was asked the following:
Write the function body of each of the methods GenStrLen, InsertChar and StrReverse for the given code below. You must take into consideration the following;
- How strings are constructed in C++
- The string must not overflow
- Insertion of character increases its length by 1
- An empty string is indicated by StrLen = 0
class Strings {
private:
char str[80];
int StrLen;
public:
// Constructor
Strings() {
StrLen=0;
};
// A function for returning the length of the string 'str'
int GetStrLen(void) {
};
// A function to inser a character 'ch' at the end of the string 'str'
void InsertChar(char ch) {
};
// A function to reverse the content of the string 'str'
void StrReverse(void) {
};
};
The answer I gave was something like this (see bellow). My one of problem is that used many extra variables and that makes me believe am not doing it the best possible way, and the other thing is that is not working….
class Strings {
private:
char str[80];
int StrLen;
int index; // *** Had to add this ***
public:
Strings(){
StrLen=0;
}
int GetStrLen(void){
for (int i=0 ; str[i]!='\0' ; i++)
index++;
return index; // *** Here am getting a weird value, something like 1829584505306 ***
}
void InsertChar(char ch){
str[index] = ch; // *** Not sure if this is correct cuz I was not given int index ***
}
void StrRevrse(void){
GetStrLen();
char revStr[index+1];
for (int i=0 ; str[i]!='\0' ; i++){
for (int r=index ; r>0 ; r--)
revStr[r] = str[i];
}
}
};
I would appreciate if anyone could explain me roughly what is the best way to have answered the question and why. Also how come my professor closes each class function like ” }; “, I thought that was only used for ending classes and constructors only.
Thanks a lot for your help.
First, the trivial
};question is just a matter of style. I do that too when I put function bodies inside class declarations. In that case the;is just an empty statement and doesn’t change the meaning of the program. It can be left out of the end of the functions (but not the end of the class).Here’s some major problems with what you wrote:
str. It’s not guaranteed to start out with\0bytes.index, you only set it withinGetStrLen. It could have value -19281281 when the program starts. What if someone callsInsertCharbefore they callGetStrLen?indexinInsertChar. What if someone callsInsertChartwice in a row?StrReverse, you create a reversed string calledrevStr, but then you never do anything with it. The string instrstays the same afterwords.The confusing part to me is why you created a new variable called
index, presumably to track the index of one-past-the-last character the string, when there was already a variable calledStrLenfor this purpose, which you totally ignored. The index of of one-past-the-last character is the length of the string, so you should just have kept the length of the string up to date, and used that, e.g.Your algorithm for string reversal, however, is just completely wrong. Think through what that code says (assuming
indexis initialized and updated correctly elsewhere). It says “for every character instr, overwrite the entirety ofrevStr, backwards, with this character”. Ifstrstarted out as"Hello World",revStrwould end up as"ddddddddddd", sincedis the last character instr.What you should do is something like this:
Take note of how that works. Say that
StrLen = 10. Then we’re copying position 0 ofstrinto position 9 ofrevStr, and then position 1 ofstrinto position9ofrevStr, etc, etc, until we copy positionStrLen - 1ofstrinto position 0 ofrevStr.But then you’ve got a reversed string in
revStrand you’re still missing the part where you put that back intostr, so the complete method would look likeAnd there are cleverer ways to do this where you don’t have to have a temporary string
revStr, but the above is perfectly functional and would be a correct answer to the problem.By the way, you really don’t need to worry about NULL bytes (
\0s) at all in this code. The fact that you are (or at least you should be) tracking the length of the string with theStrLenvariable makes the end sentinel unnecessary since usingStrLenyou already know the point beyond which the contents ofstrshould be ignored.