I saw this question on some site stating it was asked in some campus placement....so here is the solution..
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char ch[100];
int l;
int num0=0;
int num1=0;
int even=0;
puts("Enetr the string\n");
gets(ch);//need not to put & since ch is itself a pointer
l=strlen(ch);
int t=l;
if(l%2==0)
even=1;
printf("%d\n",l);
for(int i=0;i<l;i++)
{
if(ch[i]=='0')
num0++;
else
num1++;
}
if((num0==num1))
{
puts("Length of max substring having equal number of zeros and one\n");
printf("%d",l);
}
else{
if(even==0)
t=t+1;
int i=0;
num0=0;
num1=0;
int j=0;
while(t!=0)
{ int w=0;
t=t-2;
j=t;
while(j!=(l+1))
{
for( i=w;i<j;i++)
{
if(ch[i]=='0')
num0++;
else
num1++;
}
if(num0==num1)
{
printf("Maximum substring having equal number of 0's and 1's is %d between %d",w,j-1);
getch();
}
else{
num0=0;
num1=0;
w=w+1;
j=j+1;
}
}
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
char ch[100];
int l;
int num0=0;
int num1=0;
int even=0;
puts("Enetr the string\n");
gets(ch);//need not to put & since ch is itself a pointer
l=strlen(ch);
int t=l;
if(l%2==0)
even=1;
printf("%d\n",l);
for(int i=0;i<l;i++)
{
if(ch[i]=='0')
num0++;
else
num1++;
}
if((num0==num1))
{
puts("Length of max substring having equal number of zeros and one\n");
printf("%d",l);
}
else{
if(even==0)
t=t+1;
int i=0;
num0=0;
num1=0;
int j=0;
while(t!=0)
{ int w=0;
t=t-2;
j=t;
while(j!=(l+1))
{
for( i=w;i<j;i++)
{
if(ch[i]=='0')
num0++;
else
num1++;
}
if(num0==num1)
{
printf("Maximum substring having equal number of 0's and 1's is %d between %d",w,j-1);
getch();
}
else{
num0=0;
num1=0;
w=w+1;
j=j+1;
}
}
}
}
getch();
}
No comments:
Post a Comment