Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Inherited Code in C++ – Solution in Hacker Rank - hackerranksolutions8

 

Problem

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).


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, n.

Constraints :

  • 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 :

3
Peter
Me
Arxwwz

Sample Output :

Valid
Too short: 2
Invalid

Explanation :

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.


Solution :

//Inherited Code in C++ - Hacker Rank Solution
#include <iostream>
#include <string>
#include <sstream>
#include <exception>
using namespace std;

/* Define the exception here */
struct BadLengthException : exception 
{
  string s;
  BadLengthException(int n) : s(to_string(n)) {}
  const char *what() const noexcept override 
  {
    return s.c_str();
  }
};


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;
		try 
		{
			bool isValid = checkUsername(username);
			if(isValid) 
			{
				cout << "Valid" << '\n';
			} 
			else 
			{
				cout << "Invalid" << '\n';
			}
		} 
		catch (BadLengthException e) 
		{
			cout << "Too short: " << e.what() << '\n';
		}
	}
	return 0;
}

Comments