https://www.codechef.com/LRNDSA01/problems/LAPIN

Can someone tell me why string b and c are empty
In Q3 DSA
#include<bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin>>t;
for(int i=0;i<t;++i)
{
string a;
cin>>a;
int l=a.length();
string b,c;
for(int i=0;i<=l/2;++i)
{
b[i]=a[i];
c[i]=a[l-1-i];
}
//cout<<b<<" "<<c;
sort(b.begin(),b.end());
sort(c.begin(),c.end());

    if(b==c)
    cout<<"YES";
    else
    cout<<"NO";
    cout<<endl;    
}

return 0;

}

2 Likes

#include<bits/stdc++.h>
using namespace std;

int main()
{

int t;
cin>>t;
for(int i=0;i<t;++i)
{
	string a;
	cin>>a;
	int l=a.size();
	string b, c;
	for(int j=0;j<l/2;++j)
	{
		b+=a[j];
		c+=a[l-1-j];
	}
	//cout<<b<<" "<<c;
	sort(b.begin(),b.end());
	sort(c.begin(),c.end());

	if(b==c)
	cout<<"YES";
	else
	cout<<"NO";
	cout<<endl;
}

return 0;

}

I have just modified your code a little bit, this works perfectly.

1 Like

Can you tell what’s wrong in mine’s.

Strings and char arrays differ more than you think in C++.

yep

Take a visit here

Pls like

1 Like