Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Need help getting this C++ code to work in Visual Studios Question: C++ THE NOHL PALINDROME A palindrome is a word or phrase that

C++

Need help getting this C++ code to work in Visual Studios

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

//#include "stdafx.h"//

#include

#include

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<

}

// 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 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: "<

}

}

// 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 should look like this

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: ***

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions