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

Valid Username Regular Expression HackerRank Java Solution

 You are updating the username policy on your company’s internal networking platform. According to the policy, a username is considered valid if all the following constraints are satisfied:

  • The username consists of  to 30 characters inclusive. If the username consists of less than 8  or greater than 30 characters, then it is an invalid username.
  • The username can only contain alphanumeric characters and underscores (_). Alphanumeric characters describe the character set consisting of lowercase characters[a-z] , uppercase characters [A-Z], and digits[0-9] .
  • The first character of the username must be an alphabetic character, i.e., either lowercase character [a-z] or uppercase character [A-Z] .

For example:

Update the value of regularExpression field in the UsernameValidator class so that the regular expression only matches with valid usernames.

Input Format

The first line of input contains an integer n, describing the total number of usernames. Each of the next  n  lines contains a string describing the username. The locked stub code reads the inputs and validates the username.

Constraints

  • < n < 100
  • The username consists of any printable characters.

Output Format

For each of the usernames, the locked stub code prints Valid if the username is valid; otherwise Invalid each on a new line.

Sample Input 0

8
Julia
Samantha
Samantha_21
1Samantha
Samantha?10_2A
JuliaZ007
Julia@007
_Julia007

Sample Output 0

Invalid
Valid
Valid
Invalid
Invalid
Valid
Invalid
Invalid

Explanation 0

Refer diagram in the challenge statement.

Solution:-


import java.util.Scanner;


class UsernameValidator {


    public static final String regularExpression = "([a-zA-Z])(\\w){7,29}";


}




public class Solution {


    private static final Scanner scan = new Scanner(System.in);


    


    public static void main(String[] args) {


        int n = Integer.parseInt(scan.nextLine());


        while (n-- != 0) {


            String userName = scan.nextLine();



            if (userName.matches(UsernameValidator.regularExpression)) {


                System.out.println("Valid");


            } else {


                System.out.println("Invalid");


            }           


        }


    }


}

Comments