You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Inherited Code Solution

Hackerrank Inherited Code Solution

Beeze Aal
Beeze Aal

.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();
  }
};