Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: C++ THE NOHL PALINDROME A palindrome is a word or phrase that reads th

ID: 3719366 • Letter: Q

Question

Question: C++ THE NOHL PALINDROME A palindrome is a word or phrase that reads the same forward and backward...

C++

THE NOHL PALINDROME

A palindrome is a word or phrase that reads the same forward and backward. Examples of such palindromes are: “radar”, “Poor Dan is in a droop.”, “A man, a plan, a canal, Panama.”, and “a Toyota”.

A Nohl palindrome is a palindrome of at least three characters in length embedded in a word, which itself is not a palindrome. Examples of words that are Nohl palindromes are: Mississippi, banana, robot, and institution. The embedded palindrome is in bold. Examples of words that are not Nohl palindromes are: radar, roof, table, and committee.

The order of a Nohl palindrome is the length of the palindrome substring.

Your task is to accept words on input and indicate whether or not the word is a Palindrome, a Nohl palindrome, or neither. If it is a Nohl palindrome, you will further output the palindromic substring and its order. You must locate and output the Nohl palindrome with the largest order. See the examples below.

You must use functions to handle some of the chores, such as detecting Palindromes or Nohl Palindromes.

You may use any data structures you like. Hint: using char arrays might made the task easy.Complete all the tasks indicated, using functions, the data structure of your choice, commented & readable code, and a solid programming algorithm.

comments should be meaningful and based on routines

Sample run

Enter word: mississippi

mississippi is NOT a Palindrome

mississippi is a Nohl Palindrome

    Nohl string is ississi

    The order is 7

Enter word or *** to quit: tobacco

tobacco is NOT a Palindrome

tobacco is NOT a Nohl Palindrome

Enter word or *** to quit: deed

deed is a Palindrome

Enter word or *** to quit: robot

robot is NOT a Palindrome

robot is a Nohl Palindrome

    Nohl string is obo

    The order is 3

Enter word or *** to quit: ***

Press any key to continue

I have most of the code done so far for this program: It works for C but not C++. I just need help getting it to work in C++. The errors I get for C++ is

Severity Code Description Project File Line Suppression State

Error (active) E0028 expression must have a constant value ConsoleApplication1 c:UsersusfdluDesktopConsoleApplication1ConsoleApplication1ConsoleApplication1.cpp 29

Error C3863 array type 'bool [n][n]' is not assignable .

Here's the code below

// ConsoleApplication1.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include<iostream>

#include <string>

#include<cstdio>

using namespace std;

using std::string;

// A utility function to print a substring str[low..high]

void printSubStr(char* str, int low, int high)

{

for (int i = low; i <= high; ++i)

printf("%c", str[i]);

}

// This function prints the longest palindrome substring

// of str[].

// It also returns the length of the longest palindrome

void findpalidrone(char *str)

{

const int n = strlen(str); // get length of input string

// table[i][j] will be false if substring str[i..j]

// is not palindrome.

// Else table[i][j] will be true

bool table[n][n];

memset(table, 0, sizeof(table));

// All substrings of length 1 are palindromes

int maxLength = 1;

for (int i = 0; i < n; ++i)

table[i][i] = true;

// check for sub-string of length 2.

int start = 0;

for (int i = 0; i < n - 1; ++i)

{

if (str[i] == str[i + 1])

{

table[i][i + 1] = true;

start = i;

maxLength = 2;

}

}

// Check for lengths greater than 2. k is length

// of substring

for (int k = 3; k <= n; ++k)

{

// Fix the starting index

for (int i = 0; i < n - k + 1; ++i)

{

// Get the ending index of substring from

// starting index i and length k

int j = i + k - 1;

// checking for sub-string from ith index to

// jth index iff str[i+1] to str[j-1] is a

// palindrome

if (table[i + 1][j - 1] && str[i] == str[j])

{

table[i][j] = true;

if (k > maxLength)

{

start = i;

maxLength = k;

}

}

}

}

if (maxLength == 1 || maxLength == 2) {

cout << str << " is NOT a Palindrome ";

cout << str << " is NOT a Nohl Palindrome ";

}

else if (n == maxLength) {

cout << str << " is a Palindrome ";

}

else {

cout << str << " is NOT a Palindrome ";

cout << str << " is a Nohl Palindrome ";

cout << "Nohl string is ";

printSubStr(str, start, start + maxLength - 1);

printf(" The order is: %d ", maxLength);

}

}

// Driver program to test above functions

int main()

{

bool flag = true;

while (flag) {

char str[] = "";

cout << "Enter word or *** to quit: ";

cin >> str;

if (strcmp(str, "***") == 0) {

flag = false;

}

else {

findpalidrone(str);

cout << endl;

}

}

return 0;

}

Explanation / Answer

//#include "stdafx.h"//

#include<iostream>

#include <cstring>


using namespace std;

using std::string;

// A utility function to print a substring str[low..high]

//void printSubStr(char* str, int low, int high)
void printSubStr(string str,int low,int high)
{

for (int i = low; i <= high; ++i)
cout<<str[i];
//printf("%c", str[i]);

}

// This function prints the longest palindrome substring

// of str[].

// It also returns the length of the longest palindrome

//void findpalidrone(char *str)
void findpalidrone(string str)
{

//const int n = strlen(str); // get length of input string **strlen not define in c++
const int n=str.size(); // size() will return length of string

// table[i][j] will be false if substring str[i..j]

// is not palindrome.

// Else table[i][j] will be true

bool table[n][n];

memset(table, 0, sizeof(table)); //memset is define <cstring> header file

// All substrings of length 1 are palindromes

int maxLength = 1;

for (int i = 0; i < n; ++i)

table[i][i] = true;

// check for sub-string of length 2.

int start = 0;

for (int i = 0; i < n - 1; ++i)

{

if (str[i] == str[i + 1])

{

table[i][i + 1] = true;

start = i;

maxLength = 2;

}

}

// Check for lengths greater than 2. k is length

// of substring

for (int k = 3; k <= n; ++k)

{

// Fix the starting index

for (int i = 0; i < n - k + 1; ++i)

{

// Get the ending index of substring from

// starting index i and length k

int j = i + k - 1;

// checking for sub-string from ith index to

// jth index iff str[i+1] to str[j-1] is a

// palindrome

if (table[i + 1][j - 1] && str[i] == str[j])

{

table[i][j] = true;

if (k > maxLength)

{

start = i;

maxLength = k;

}

}

}

}

if (maxLength == 1 || maxLength == 2) {

cout << str << " is NOT a Palindrome ";

cout << str << " is NOT a Nohl Palindrome ";

}

else if (n == maxLength) {

cout << str << " is a Palindrome ";

}

else {

cout << str << " is NOT a Palindrome ";

cout << str << " is a Nohl Palindrome ";

cout << "Nohl string is ";

printSubStr(str, start, start + maxLength - 1);

cout<<" The order is: "<<maxLength<<" "; // cout for print a statement instead of printf
//printf(" The order is: %d ", maxLength);  

}

}

// Driver program to test above functions

int main()

{

bool flag = true;

while (flag) {

//char str[] = "";
string str; //string class

cout << "Enter word or *** to quit: ";

cin >> str;
//if (strcmp(str, "***") == 0) strcmp is not define in c++
if (str.compare( "***") == 0) {

flag = false;

}

else {

findpalidrone(str);

cout << endl;

}

}

return 0;

}
/*OUTPUT

Enter word or *** to quit: mississippi
mississippi is NOT a Palindrome
mississippi is a Nohl Palindrome
Nohl string is ississi
The order is: 7

Enter word or *** to quit: tobacco
tobacco is NOT a Palindrome
tobacco is NOT a Nohl Palindrome

Enter word or *** to quit: deed
deed is a Palindrome

Enter word or *** to quit: robot
robot is NOT a Palindrome
robot is a Nohl Palindrome
Nohl string is obo
The order is: 3

Enter word or *** to quit: ***

--------------------------------
Process exited after 47.41 seconds with return value 0
Press any key to continue . . .
*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote