Programming Articles - Page 2165 of 3366

Types of quantifiers in Java regex

Maruthi Krishna
Updated on 13-Jul-2020 12:14:40

860 Views

If you want to specify the number of occurrences while constructing a regular expression you can use quantifiers. Java supports three types of quantifiers namely: greedy quantifiers, reluctant quantifiers and possessive quantifiers.Greedy quantifiers − Greedy quantifiers are the default quantifiers. A greedy quantifier matches as much as possible from the input string (longest match possible) if match not occurred it leaves the last character and matches again.Exampleimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter input text: ");     ... Read More

Posix character classes p{Print} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 10:22:45

1K+ Views

This class matches all the printable characters.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrintableCharacters {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{Print}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0; ... Read More

Posix character classes p{Graph} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 10:12:14

598 Views

This class matches all visible characters i.e. alphabets, digits, punctuation marks.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class VisiblieCharacters {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{Graph}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       ... Read More

Missing Number In Arithmetic Progression using C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:13:44

347 Views

Suppose we have an array that represents elements of arithmetic progression in order. One element is missing. We have to find the missing element. So if arr = [2, 4, 8, 10, 12, 14], output is 6, as 6 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the difference between the middle and next to the middle is the same as diff or not. If not, then the missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the ... Read More

Day of the Week in C++

Arnab Chakraborty
Updated on 19-Feb-2021 07:24:45

7K+ Views

Suppose we have a date (day, month and year). From this date, we have to find the day of the week of that given date. To solve this we will use Zeller’s Algorithm. The formula to find weekday using Zeller’s Algorithm is here𝑤=$$\lgroup d+\lfloor \frac{13(m+1)}{5} \rfloor+y+\lfloor\frac{y}{4} \rfloor+\lfloor\frac{c}{4} \rfloor+5c \rgroup mod 7$$The formula is containing some variables; They are −d − The day of the date.m − It is the month code. For March to December, it is 3 to 12, for January it is 13, and for February it is 14. When we consider January or February, then the given ... Read More

N-th Tribonacci Number in C++

Arnab Chakraborty
Updated on 28-Apr-2020 11:07:03

416 Views

Suppose we have a value n, we have to generate n-th Tribonacci number. The Tribonacci numbers are similar to the Fibonacci numbers, but here we are generating a term by adding three previous terms. Suppose we want to generate T(n), then the formula will be like below −T(n) = T(n - 1) + T(n - 2) + T(n - 3)The first few numbers to start, are {0, 1, 1}We can solve them by following this algorithm −Algorithm• first := 0, second := 1, third := 1 • for i in range n – 3, do    o next := first ... Read More

Hamming Distance in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:05:39

4K+ Views

Consider we have two integers. We have to find the Hamming distance of them. The hamming distance is the number of bit different bit count between two numbers. So if the numbers are 7 and 15, they are 0111 and 1111 in binary, here the MSb is different, so the Hamming distance is 1.To solve this, we will follow these steps −For i = 31 down to 0b1 = right shift of x (i AND 1 time)b2 = right shift of y (i AND 1 time)if b1 = b2, then answer := answer + 0, otherwise answer := answer + ... Read More

Fizz Buzz in Python

Arnab Chakraborty
Updated on 28-Apr-2020 11:03:59

4K+ Views

Suppose we have a number n. We have to display a string representation of all numbers from 1 to n, but there are some constraints.If the number is divisible by 3, write Fizz instead of the numberIf the number is divisible by 5, write Buzz instead of the numberIf the number is divisible by 3 and 5 both, write FizzBuzz instead of the numberTo solve this, we will follow these steps −For all number from 1 to n, if a number is divisible by 3 and 5 both, print “FizzBuzz”otherwise when the number is divisible by 3, print “Fizz”otherwise when ... Read More

Posix character classes p{Punct} Java regex

Maruthi Krishna
Updated on 09-Jan-2020 09:59:48

3K+ Views

This class matches punctuation marks. i.e.!"#$%&'()*+, -./:;?@[\]^_`{|}~Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class AlphanumericExample {    public static void main(String args[]) {       //Reading String from user       System.out.println("Enter a string");       Scanner sc = new Scanner(System.in);       String input = sc.nextLine();       //Regular expression       String regex = "[\p{Punct}]";       //Compiling the regular expression       Pattern pattern = Pattern.compile(regex);       //Retrieving the matcher object       Matcher matcher = pattern.matcher(input);       int count = 0; ... Read More

How to implement ToDoubleFunction using lambda expression in Java?

raja
Updated on 13-Jul-2020 12:10:26

795 Views

ToDoubleFunction is a functional interface defined in java.util.function package. This functional interface expects a parameter as input and produces a double-valued result. It is used as an assignment target for a lambda expression or method reference. ToDoubleFunction interface contains only one abstract method, applyAsDouble().Syntax@FunctionalInterface public interface ToDoubleFunction {    double applyAsDouble(T value); }Example-1import java.util.function.ToDoubleFunction; public class ToDoubleFunctionTest1 {    public static void main(String args[]) {       ToDoubleFunction strLength = s -> s.length();    // lambda expression       System.out.println("The length of a given String using lambda expression is: " + strLength.applyAsDouble("TutorialsPoint"));       ToDoubleFunction innerClassImplementation = new ToDoubleFunction() ... Read More

Advertisements