Answer to Question #288542 in C++ for madhu

Question #288542

Given a string S. Let us divide 5 into two equal parts s1 and s2.s is called a halindrome if at least any one of the following conditions satisfy:


1.Sis a palindrome and length of S>=2


2.S1 is a halindrome.


3S2 a halindrome.


In the case of an odd length string the middle element is not present in both S1 and S2. If index of middle element is m. then, S1= S [0,m-1] and S2=S[m+1|S|-1].


Input Specification


input 1: Number of strings 1<=input1<=100.

imput2:An array of size input1 containing strings 2<=length of each string<=100.

output specification:

for each test case, return the number of strings which are halindromes.

EX:input1:1

input2:{harshk}

output 0

explanation:1.string S is not forming a palindrome.

2.string S1=har,which is not a halindrome.

3.String S2=shk,which is not a halindrome.

as none of the conditions are true,hence output to be returned is 0.


1
Expert's answer
2022-01-18T13:03:00-0500
#include<iostream>
#include<string>

using namespace std;

bool IsHalindrome(string str)
{
	if (str.size() < 2)
		return false;
	string s = str;
	reverse(s.begin(), s.end());
	if (s == str)
		return true;


	int m;
	string s1;
	string s2;
	if (str.size() % 2 == 0)
	{
		m = str.size() / 2;
		s1 = str.substr(0, m);
		s2 = str.substr(m, m);
	}
	else
	{
		m = str.size() / 2;
		s1 = str.substr(0, m - 1);
		s2 = str.substr(m + 1, m);
	}


	bool IsHalS1 = true;
	bool IsHalS2 = true;


	if (s1.size() < 2)
		IsHalS1 = false;
	if (s2.size() < 2)
		IsHalS2 = false;
	if (!IsHalS1 && !IsHalS2)
		return false;
	if (IsHalS1)
	{
		s = s1;
		reverse(s.begin(), s.end());
		if (s == s1)
			return true;
	}
	if (IsHalS2)
	{
		s = s2;
		reverse(s.begin(), s.end());
		if (s == s2)
			return true;
	}
	return false;
}


int main()
{
	int input1;
	cout << "Please, enter the number of strings (from 1 to 100) : ";
	cin >> input1;
	if (input1 < 1 || input1>100)
	{
		cout << "Incorrext input!";
		return -1;
	}
	string* arrStr = new string[input1];
	cout << "\nInput strings:";
	for (int i = 0; i < input1; i++)
	{
		cin >> arrStr[i];
	}
	int count = 0;
	for (int i = 0; i < input1; i++)
	{
		 if(IsHalindrome(arrStr[i]))count++;
	}
	cout << "Number of halindromes is " << count;




}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment