Kartik Bhaiya and Strings

Hello,
I am not satisfied with the reply so reopening this doubt, I had already had a look into the given solution, but I wanted to know what is the mistake I am making in the code I am writing, can you pl, point out that ?

Attaching the code for your reference:

#include
#include
#include
using namespace std;
int largest(int var_1, int var_2)
{
if(var_1>var_2)
{
return var_1;
}
else
{
return var_2;
}
/else{
return var_1;
}
/
}
int count_b_func(int k, char temp[])
{
int len = strlen(temp);
int count_b = 0;
int a = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==‘b’)
{
count_b++;
}
if(temp[i]==‘a’)
{
a++;
}

}

if(k>a)
{
	count_b = count_b+a;
}
else if(k<=a)
{
	count_b = count_b+k;

}

return count_b;

}

int count_a_func(int k, char temp[])
{
int len = strlen(temp);
int count_a = 0;
int b = 0;
for (int i =0;i<=len-1;i++)
{
if(temp[i]==‘a’)
{
count_a++;
}
if(temp[i]==‘b’)
{
b++;
}

}

if(k>b)
{
	count_a = count_a+b;
}
else if(k<=b)
{
	count_a = count_a+k;

}

return count_a;

}

int main() {
int len_a =0;
int len_b =0;
int k;
cin>>k;
cin.get();
char a[1000];
char temp[1000];
cin.getline(a,1000);
int len = strlen(a);
int j;
int a_num;
int b_num;
int perf;
//1.getting substrings
for(int i=0;i<=len-1;i++)
{
int x=0;
for(j =i;j<=len-1;j++)
{
temp[x++] = a[j]; //substr stored in temp arr.

	}
	temp[j] = '\0';
	//cout<<temp<<endl;
	a_num = count_a_func(k,temp);
	//cout<<"No of a "<<a_num<<endl;
	b_num = count_b_func(k,temp);
	//cout<<"No of b "<<b_num<<endl;
	len_a = largest(a_num,len_a);
	len_b = largest(b_num,len_b);


}
perf = largest(len_a,len_b);
cout<<perf;

return 0;

}

hi @agganushka_9eb7cecc88bfac61
i have provided u with a step wise approach, kindly go through it and try to dry run on sample input, u will get better insight…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.