Write a program to find all the patterns of 0(1+)0 in the given string
ADVERTISEMENTS
Write a program to find all the patterns of 0(1+)0
in the given string. In this program, you will learn how to find all the patterns of 0(1+)0
in the given string.
To understand this example, you should have knowledge of these c programming topics:
- While loop
- Functions
Let us consider this program through these examples in C, C++, C#, Java, Python, and PHP language.
// C program to find all the patterns of 0(1+)0 in the given string
#include <stdio.h>
#include <string.h>
// @This function will find the pattern
int findPattern(char str[]) {
char last = str[0];
int i = 1, c = 0;
// c is the counter part
while (i < strlen(str)) {
if (str[i] == '1' && last == '0') {
while (str[i] == '1')
i++;
if (str[i] == '0')
c++;
}
last = str[i];
i++;
}
return c;
}
// @It's the driver function
int main() {
char str[50];
printf("Enter the string: ");
gets(str);
printf("\nThe Number of patterns found: %d\n", findPattern(str));
return 0;
}
// C++ program to find all the patterns of 0(1+)0 in the given string
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
// @This function will find the pattern
int findPattern(char str[]) {
char last = str[0];
int i = 1, c = 0;
// c is the counter part
while (i < strlen(str)) {
if (str[i] == '1' && last == '0') {
while (str[i] == '1')
i++;
if (str[i] == '0')
c++;
}
last = str[i];
i++;
}
return c;
}
// @It's the driver function
int main() {
char str[50];
cout << "Enter the string: ";
cin >> str;
cout << "\nThe Number of patterns found: " << findPattern(str) << "\n";
return 0;
}
// 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;
}
}
// C# program to find all the patterns of 0(1+)0 in the given string
using System;
public class W3CW {
// @It's the driver function
static public void Main (string[] args) {
Console.Write("Enter the string: ");
string str = Console.ReadLine();
Console.WriteLine("\nThe Number of patterns found: " + findPattern(str));
}
// @This function will find the pattern
static public int findPattern(string str) {
char last = str[0];
int i = 1, c = 0;
// c is the counter part
while (i < str.Length) {
if (str[i] == '1' && last == '0') {
while (i < str.Length && str[i] == '1')
i++;
if (i < str.Length && str[i] == '0')
c++;
}
last = i < str.Length ? str[i] : last;
i++;
}
return c;
}
}
# Python program to find all the patterns of 0(1+)0 in the given string
# @This function will find the pattern
def findPattern(str):
last, i, c = str[0], 1, 0
# c is the counter part
while i < len (str):
if str[i] == '1' and last == '0':
while i < len (str) and str[i] == '1':
i += 1
if i < len (str) and str[i] == '0':
c += 1
last = str[i] if i < len (str) else last
i += 1
return c
# @It's the driver code
print ("Enter the string: ", end="")
str = str (input ())
print ("\nThe Number of patterns found: ", findPattern(str))
<?php
// PHP program to find all the patterns of 0(1+)0 in the given string
// @This function will find the pattern
function findPattern($str) {
$last = $str[0];
$i = 1;
$c = 0;
// $c is the counter part
while ($i < strlen($str)) {
if ($str[$i] == '1' && $last == '0') {
while ($i < strlen($str) && $str[$i] == '1') {
$i++;
}
if ($i < strlen($str) && $str[$i] == '0') {
$c++;
}
}
$last = $i < strlen($str) ? $str[$i] : $last;
$i++;
}
return $c;
}
// @It's the driver code
$str = "1001001001";
echo "The Number of patterns found: " . findPattern($str) . "\n";
?>
Output:
Enter the string: 1001001001
The Number of patterns found: 2