Hackerrank Inherited Code Solution
2 min read

Hackerrank Inherited Code Solution

Hackerrank Inherited Code Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

You inherited a piece of code that performs username validation for your company's website. The existing function works reasonably well, but it throws an exception when the username is too short. Upon review, you realize that nobody ever defined the exception.

The inherited code is provided for you in the locked section of your editor. Complete the code so that, when an exception is thrown, it prints Too short: n (where  is the length of the given username).

Input Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

The first line contains an integer, , the number of test cases.
Each of the  subsequent lines describes a test case as a single username string, .

Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

  • The username consists only of uppercase and lowercase letters.

Output Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

You are not responsible for directly printing anything to stdout. If your code is correct, the locked stub code in your editor will print either Valid (if the username is valid), Invalid (if the username is invalid), or Too short: n (where  is the length of the too-short username) on a new line for each test case.

Sample Input

3
Peter
Me
Arxwwz

Sample Output

Valid
Too short: 2
Invalid

Explanation

Username Me is too short because it only contains  characters, so your exception prints .
All other validation is handled by the locked code in your editor.

Solution in cpp

Approach 1.

/* Define the exception here */
class BadLengthException: public exception {
    int N;
    public:
    BadLengthException(int N){this->N = N;}
    int what(){return N;}
};

Approach 2.

/* Define the exception here */
class BadLengthException: public exception{
private:
    int N;
public:
    BadLengthException(int N){this->N = N;};
    int what(){return N;};
};

Approach 3.

/* Define the exception here */
struct BadLengthException : public exception
{
  int n;
  BadLengthException(int number)
    : n(number)
  {}

  const char * what () const throw ()
  {
    return to_string(n).c_str();
  }
};

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×