Question

Convert this C++ code to C to print all possible palindromes of a string #include <bits/stdc++.h>...

Convert this C++ code to C to print all possible palindromes of a string


#include <bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
bool isPalindrome(string strData, int first, int last)
{
while (first < last)
{
if (strData[first] != strData[last])
return false;
first++;
last--;
}
return true;
}



void allPalPartUtility(vector<vector<string> >&allPart, vector<string> &currentPart, int startPos, int endPos, string strData)
{
if (startPos >= endPos)
{
allPart.push_back(currentPart);
return;
}
for (int co = startPos; co < endPos; co++)
{
if (isPalindrome(strData, startPos, co))
{
currentPart.push_back(strData.substr(startPos, co - startPos + 1));
allPalPartUtility(allPart, currentPart, co + 1, endPos, strData);
currentPart.pop_back();
}
}
}



void allPalPartitions(string strData)
{
int no = strData.length();
int ro, co;
vector<vector<string> > allPart;
vector<string> currentPart;
allPalPartUtility(allPart, currentPart, 0, no, strData);
cout<<"\n Palindromic decomposition of string: \n";
for (ro = 0; ro < allPart.size(); ro++ )
{
for (co = 0; co < allPart[ro].size(); co++)
cout << allPart[ro][co] << ", ";
cout << "\n";
}
cout<<"\n string "<<strData<<" has "<<ro<<" palindromic decompositions";
}



int main()
{
string stringData;
while(1)
{
cout<<"\n Enter a string: ";


cin>>stringData;
if (stringData.compare("-1") == 0)
break;
allPalPartitions(stringData);
}
return 0;
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <bits/stdc++.h>
#include <iostream>
#include<vector>
using namespace std;
bool isPalindrome(string strData, int first, int last)
{
while (first < last)
{
if (strData[first] != strData[last])
return false;
first++;
last--;
}
return true;
}


void allPalPartUtility(vector<vector<string> >&allPart, vector<string> &currentPart, int startPos, int endPos, string strData)
{
if (startPos >= endPos)
{
allPart.push_back(currentPart);
return;
}
for (int co = startPos; co < endPos; co++)
{
if (isPalindrome(strData, startPos, co))
{
currentPart.push_back(strData.substr(startPos, co - startPos + 1));
allPalPartUtility(allPart, currentPart, co + 1, endPos, strData);
currentPart.pop_back();
}
}
}


void allPalPartitions(string strData)
{
int no = strData.length();
int ro, co;
vector<vector<string> > allPart;
vector<string> currentPart;
allPalPartUtility(allPart, currentPart, 0, no, strData);
cout<<"\n Palindromic decomposition of string: \n";
for (ro = 0; ro < allPart.size(); ro++ )
{
for (co = 0; co < allPart[ro].size(); co++)
cout << allPart[ro][co] << ", ";
cout << "\n";
}
cout<<"\n string "<<strData<<" has "<<ro<<" palindromic decompositions";
}


int main()
{
string stringData;
while(1)
{
cout<<"\n Enter a string: ";

cin>>stringData;
if (stringData.compare("-1") == 0)
break;
allPalPartitions(stringData);
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Convert this C++ code to C to print all possible palindromes of a string #include <bits/stdc++.h>...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip>...

    my program wont run on my computer and im not understanding why. please help. #include<iostream> #include<iomanip> #include<string> #include "bookdata.h" #include "bookinfo.h" #include "invmenu.h" #include "reports.h" #include "cashier.h" using namespace std; const int ARRAYNUM = 20; BookData bookdata[ARRAYNUM]; int main () { bool userChoice = false; while(userChoice == false) { int userInput = 0; bool trueFalse = false; cout << "\n" << setw(45) << "Serendipity Booksellers\n" << setw(39) << "Main Menu\n\n" << "1.Cashier Module\n" << "2.Inventory Database Module\n" << "3.Report Module\n"...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • Fix the following code to print whether the input is positive or negative #include <iostream> #include...

    Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){    int y; int result; cin >> y; result = positiveOrNegative(y);    return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string...

    Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() {    string first, last, job;    double hours, wages, net, gross, tax, taxrate = .40;    double oPay, oHours;    int deductions;    // input section    cout << "Enter First Name: ";    cin >> first;    cout << "Enter Last Name: ";    cin >> last;    cin.ignore();    cout << "Enter Job Title: ";    getline(cin, job);    cout << "Enter Hours Worked:...

  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0));...

    #include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....

  • #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car {...

    #include<iostream> #include<string> #include<iomanip> using namespace std; /* ********* Class Car ************* ********************************* */ class Car { private: string reportingMark; int carNumber; string kind; bool loaded; string choice; string destination; public: Car() { reportingMark = ""; carNumber = 0; kind = "Others"; loaded = 0; destination = "NONE"; } ~Car() { } void setUpCar(string &reportingMark, int &carNumber, string &kind, bool &loaded, string &destination); }; void input(string &reportingMark, int &carNumber, string &kind, bool &loaded,string choice, string &destination); void output(string &reportingMark, int &carNumber,...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT