Java Online Compiler
Example: Java program to find all the patterns of 0(1+)0 in the given string
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Java program to find all the patterns of 0(1+)0 in the given string import java.util.Scanner; public class Main { // @It's the driver function public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the string: "); String str = in.nextLine(); System.out.println("\nThe Number of patterns found: " + findPattern(str)); } // @This function will find the pattern public static int findPattern(String str) { char last = str.charAt(0); int i = 1, c = 0; // c is the counter part while (i < str.length()) { if (str.charAt(i) == '1' && last == '0') { while (str.charAt(i) == '1') i++; if (str.charAt(i) == '0') c++; } last = str.charAt(i); i++; } return c; } }
1001001001
Output
Clear
ADVERTISEMENTS