Write a C program to find the number of patterns formed of 0(1+)0 in the given string. For example, 010, 0110, 01110, etc. To implement this, the input necessary is a string on which the number of 0(1+)0 patterns formed is to be calculated. Our desired output is an integer number denoting the number of such patterns formed on the input string.
The first step after understanding of problem statement completely is, gathering all the necessary inputs. Here, to read our input string (c), we can use the predefined function, gets(). This function reads a line of input and stores it in a string pointer or character array.
It continues to read the input until it reaches either a new line or end-of-file. Therefore, by using this gets() function, we read our input string and store it in ‘c’.
After getting our input string, we have to iterate from beginning to the end i.e., from 0 to end-of-file and check for the presence of this pattern. To check this, we’ll be using a flag variable (t) which is initially zero to control the flow.
In the loop, we check for whether the character (c[i]) is equal to zero. If yes then there are three possibilities- it is either the the beginning zero of the pattern or the ending zero of the pattern or it doesn’t belong to the pattern only.
To determine which of the above case it is, we’ll check the value of t, if t is equal to 1 then, the character (c[i]) is the ending zero hence, we increment the count variable (j) which is initially zero by 1. After incrementing this, we’ll again bring back the value to t to zero for checking the next pattern present in the string.
If the character is zero and its next character is 1 then, it indicates that this zero is the beginning of the pattern hence, we change the t value to 1 again.
int i,j=0,t=0;
for(i=0;c[i];i++ ) {
if(c[i]==’0′) {
if(t==1) {
j++;
t=0;
}
if(c[i+1]==’1′)
t=1;
}
}
This way, for every occurrence of zero in the string, we check which case it belongs to and keep counting the number of patterns present in the given input string.
After completion of all the iterations, when we exit the loop then, the value of count variable (j) is nothing but, our desired resultant output i.e., the number of patterns of 0(1+)0 present in the given input string. We display this value using predefined output function, printf() on the console screen.
printf(“Enter a string : %d”,j);
0(1+)0 in The Given String Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <string.h> int main() { char c[150]; int i,j=0,t=0; printf("Enter a string : "); gets(c); i=0; for(i=0;c[i];i++ ) { if(c[i]=='0') { if(t==1) { j++; t=0; } if(c[i+1]=='1') t=1; } } printf("Enter a string : %d",j); return 0; } |
Output:
1 2 |
Enter a string : 10101010101101111 Enter a string : 5 |
Using Function
In the above scenario, we have seen the logic to determine the number of patterns of 0(1+)0 present in the given string but since the entire code is within main method itself, there are a few drawbacks. If some part of the code is to be found, we’ll have to go through entire main method looking for it so, it is not quite readable. Apart from that, if we might need to use the same logic elsewhere in the code, we’ll have to rewrite it.
To get over this, we can make use of functions. In both the cases the time and space complexity would be the same as we are making use of the same logic.
But the advantage here is that, when we use functions we make the code reusable as well as enhance the readability of the code.
If we split the code based on logic and functionality and, place it within separate function blocks, if same logic is required elsewhere we can just make a function call instead of rewriting it.
Also, as the code is now systematically placed based on the functionality, the code becomes cleaner and more readable.
To implement this, we can split our code into two parts- the i/o operations and the logic. In main method, we can read the input string using gets() and then, call the use-defined function (count) passing this input string as parameter.
This function contains the same logic discussed above and returns the number of such patterns in the string passed as parameter to the main method. This count can then be displayed on the output console screen in the main method. This way, the main method has all the i/o functions while, the user-defined function (count) has the logic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include <string.h> int count(char *c) { int i,j=0,n,t=0; for(i=0;c[i];i++ ) { if(c[i]=='0') { if(t==1) { j++; t=0; } if(c[i+1]=='1') t=1; } } return j; } int main() { char c[150]; printf("Enter a string : "); gets(c); printf("Enter a string : %d",count(c)); return 0; } |
Output:
1 2 |
Enter a string : 101010110101001110011010101111110 Enter a string : 10 |