Answer to Question #132462 in C++ for Ayesha

Question #132462
Write C++ functions to perform the following tasks.
(i) palindrome(A, size) – this function receives one dimensional character array
‘A ‘ of size ‘ size ‘ as input and returns a character ‘P’
or ‘ N’ respectively, depending on whether the word is a
palindrome or not. If the entered word and the reverse
of it have the same pattern, then the word is a palindrome.
eq: madam
1
Expert's answer
2020-09-10T09:20:04-0400
#include <iostream>
#include <cstring>

char palindrome(const char* A, size_t size)
{
    const char* begin = A;
	const char* end   = A + size - 1;

	while(begin < end)
	{
		if(*begin++ != *end--)
		{
			return 'N';
		}
	}

	return 'P';
}

int main()
{
	const char* test = "madam"; 
	std::cout << "'" << test << "' - " << palindrome(test, strlen(test)) << "\n";

    return 0;
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS