Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 800113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:14:28+00:00 2026-05-14T23:14:28+00:00

I just took an exam where I was asked the following: Write the function

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:14:28+00:00Added an answer on May 14, 2026 at 11:14 pm

    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:

    1. You never initialize the contents of str. It’s not guaranteed to start out with \0 bytes.
    2. You never initialize index, you only set it within GetStrLen. It could have value -19281281 when the program starts. What if someone calls InsertChar before they call GetStrLen?
    3. You never update index in InsertChar. What if someone calls InsertChar twice in a row?
    4. In StrReverse, you create a reversed string called revStr, but then you never do anything with it. The string in str stays 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 called StrLen for 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.

    int GetStrLen(void){
      return StrLen; 
    }
    
    void InsertChar(char ch){    
      if (StrLen < 80) {
        str[StrLen] = ch;
        StrLen = StrLen + 1; // Update the length of the string
      } else {
        // Do not allow the string to overflow. Normally, you would throw an exception here
        // but if you don't know what that is, you instructor was probably just expecting
        // you to return without trying to insert the character.
        throw std::overflow_error();
      }
    }
    

    Your algorithm for string reversal, however, is just completely wrong. Think through what that code says (assuming index is initialized and updated correctly elsewhere). It says “for every character in str, overwrite the entirety of revStr, backwards, with this character”. If str started out as "Hello World", revStr would end up as "ddddddddddd", since d is the last character in str.

    What you should do is something like this:

    void StrReverse() {
       char revStr[80];
       for (int i = 0; i < StrLen; ++i) {
         revStr[(StrLen - 1) - i] = str[i];
       }
    }
    

    Take note of how that works. Say that StrLen = 10. Then we’re copying position 0 of str into position 9 of revStr, and then position 1 of str into position 9 of revStr, etc, etc, until we copy position StrLen - 1 of str into position 0 of revStr.

    But then you’ve got a reversed string in revStr and you’re still missing the part where you put that back into str, so the complete method would look like

    void StrReverse() {
       char revStr[80];
       for (int i = 0; i < StrLen; ++i) {
         revStr[(StrLen - 1) - i] = str[i];
       }
       for (int i = 0; i < StrLen; ++i) {
         str[i] = revStr[i];
       }
    }
    

    And 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 the StrLen variable makes the end sentinel unnecessary since using StrLen you already know the point beyond which the contents of str should be ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.