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> ¤tPart, 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;
}
#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> ¤tPart, 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;
}
Convert this C++ code to C to print all possible palindromes of a string #include <bits/stdc++.h>...
#include <iostream> #include <stack> #include <queue> using namespace std; void printFromStack(string expr){ stack<char> myStack; for(int i=0; i<expr.length(); i++){ //Insert code here to push each character onto the stack } cout << "My stack is popped in this order" << endl; while(!myStack.empty()){ //Insert code here to cout the top of the stack one by one //Pop each one after it’s printed out } cout << endl; } void printFromQueue(string expr){ queue<char> myQueue; //Insert code here to push each character onto the...
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 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 <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 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 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> #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)); 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 { 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,...
previous assignment code /*C++ test program to test the classes, Animal, Mammal and Cat and print the results on console window.*/ //Main.cpp //include header files #include<iostream> //include Animal, Mammal and Cat header files #include "Animal.h" #include "Mammal.h" #include "Cat.h" using namespace std; int main() { //create Animal object Animal animal("Dog","Mammal",4); cout<<"Animal object details"<<endl; cout<<"Name: "<<animal.getName()<<endl; cout<<"Type: "<<animal.getType()<<endl; cout<<"# of Legs: "<<animal.getLegs()<<endl; cout<<endl<<endl; //create Cat object Cat cat("byssinian cat","carnivorous mammal",4,"short...