Program to find all the patterns of 0(1+)0 in the given string | faceprep

Program to find all the patterns of 0(1+)0 in the given string is discussed here. Given a string containing 0’s and 1’s, find the total number of 0(1+)0 patterns in the string and output it.

0(1+)0 – There should be at least one ‘1’ between the two 0’s.
For example, consider the following string. 

Input: 01101111010
Output: 3
Explanation: 

01101111010 – count = 1
01101111010 – count = 2
01101111010 – count = 3

Algorithm to find all the patterns of 0(1+)0 in the given string

  • Input the given string.
  • Scan the string, character by character.
  • If the given pattern is encountered, increment count.
  • Print count.
Program to find all the patterns of 0(1+)0 is given below.

/* C program to find all the patterns of 0(1+)0 in the given string */
#include <stdio.h>
#include <stdlib.h>

/* Function to count the patterns */
int find_pattern(char str[])
{
char last = str[0];
int i = 1, counter = 0;
while (i < strlen(str))
{
/* We found 1 and last character was ‘0’, state change*/
if (str[i] == ‘1’ && last == ‘0’)
{
while (str[i] == ‘1’)
i++;

/* After the stream of 1’s, we got a ‘0’, counter incremented*/
if (str[i] == ‘0’)
counter++;
}
/* Store the last character */
last = str[i];
i++;
}
return counter;
}

int main()
{
char str[50];
printf(“\nEnter the string : “);
gets(str);
printf(“\nNumber of patterns found : %d”, find_pattern(str));

printf(“\n”);
return 0;
}

/* C++ program to find all the patterns of 0(1+)0 in the given string */
#include <bits/stdc++.h>
using namespace std;

/* Function to count the patterns */
int find_pattern(string str)
{
char last = str[0];
int i = 1, counter = 0;
while (i < str.size())
{
/* We found 1 and last character was ‘0’, state change*/
if (str[i] == ‘1’ && last == ‘0’)
{
while (str[i] == ‘1’)
i++;

/* After the stream of 1’s, we got a ‘0’, counter incremented*/
if (str[i] == ‘0’)
counter++;
}
/* Store the last character */
last = str[i];
i++;
}
return counter;
}

int main()
{
string str;
cout << “\nEnter the string : “;
cin >> str;
cout << “Number of patterns found : ” << find_pattern(str) << endl;
return 0;
}

/* Java program to find all the patterns of 0(1+)0 in the given string */

import java.util.*;
public class Main
{
static int find_pattern(char str[])
{
char last = str[0];
int i = 1, counter = 0;
while (i < str.length)
{
/* We found 1 and last character was ‘0’, state change*/
if (str[i] == ‘1’ && last == ‘0’)
{
while (str[i] == ‘1’)
i++;

/* After the stream of 1’s, we got a ‘0’, counter incremented*/
if (str[i] == ‘0’)
counter++;
}
/* Store the last character */
last = str[i];
i++;
}
return counter;
}

public static void main (String args[])
{
String string;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the string : “);
string = sc.next();
char[] str = string. toCharArray();
System.out.print(“\nNumber of patterns : ” + find_pattern(str));
}
}

# Python program to find all the patterns of 0(1+)0 in the given string

def find_pattern(str):
last = str[0]

i = 1; counter = 0
while (i < len(str)):
if (str[i] == ‘1’ and last == ‘0’):
while (str[i] == ‘1’):
i += 1
if (str[i] == ‘0’):
counter += 1
last = str[i]
i += 1

return counter


str = input(“Enter the string : “)
ans = find_pattern(str)
print(“Number of patterns : “,ans)

Output:

find all the patterns of 010 in the given string