Inherited Code Hackerrank Solution in C++ | Classes Solution

Inherited Code Hackerrank Solution in C++. 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 n is the length of the given username).

Inherited Code Hackerrank Solution in C++

Input Format

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


  • 1 <= t <= 1000
  • 1 <= |u| <= 100
  • The username consists only of uppercase and lowercase letters.

Output Format

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 n is the length of the too-short username) on a new line for each test case.

Sample Input


Sample Output

Too short: 2


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

Submit your solution here: Click here

Inherited Code Hackerrank Solution in C++

#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;

/*Define the exception here */

class BadLengthException
		int n;
		BadLengthException(int errornumber)
			n = errornumber;

	int what()
		return n;

bool checkUsername(string username)
	bool isValid = true;
	int n = username.length();
	if (n < 5)
		throw BadLengthException(n);

	for (int i = 0; i < n - 1; i++)
		if (username[i] == 'w' && username[i + 1] == 'w')
			isValid = false;

	return isValid;

int main()
	int T;
	cin >> T;
	while (T--)
		string username;
		cin >> username;
			bool isValid = checkUsername(username);
			if (isValid)
				cout << "Valid" << '\n';
				cout << "Invalid" << '\n';

		catch (BadLengthException e)
			cout << "Too short: " << e.what() << '\n';

	return 0;

The Output of Inherited Code Hackerrank Solution

The Output of Inherited Code Hackerrank Solution

Similar to Inherited Code

  1. Box It Hackerrank Solution in C++
  2. Classes and Objects Hackerrank Solution in C++
  3. Class Hackerrank Solution in C++
  4. Virtual Functions Hackerrank Solution in C++
  5. C++ Class Templates Hackerrank Solution in C++
  6. Operator Overloading Hackerrank Solution in C++
  7. Preprocessor Solution Hackerrank Solution in C++
  8. Multi-Level Inheritance Hackerrank Solution in C++
  9. Accessing Inherited Functions Hackerrank Solution in C++
  10. Print Pretty Hackerrank Solution in C++
  11. Maps STL Hackerrank Solution in C++
  12. Sets STL Hackerrank Solution in C++
Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me