Exercise 3 [20 points] A palindrome is a sequence of characters which reads the
ID: 3723867 • Letter: E
Question
Exercise 3 [20 points] A palindrome is a sequence of characters which reads the same backward or forward. Examples: "rotor", "Rotor" (with capital R), "step on no pets", ete Write the code for the following functions: // returns true if s is a palindrome and false otherwise bool is_palidrome(string s); // returns the number of palindromes in vector v of strings int countPalindromes(const vectorcstring& v) Exercise 4120 points] . Write a declaration for a structure named Inventoryltem that holds the following data: inventory item ID or code, inventory item description, and item availability (i.e. number of available items). For example, if printer cables are part of your inventory, you will hold the following data: 120 (inventory item ID). "Printer Cable" (inventory item description), and 235 (number of printer cables available). Write the following function whose prototype is given below: // nicely prints the data held in item one piece of data per line void display[Inventoryltem item); Exercise 5 [40 points] Consider the following class Inventoryltem class Inventoryltem public: // constructor Inventoryltem(): ID(O),_descriptionl"no name"), _onHand(0) D // get functions int getltemID() const; string getitemDescription() const int getltemAvailability) const;Explanation / Answer
3.
bool is_Palindrome(string s){
for(int i=0,j=s.length()-1;i<=j;i++,j--){
if(s[i]!=s[j])
return false;
}
return true;
}
int countPalindromes(const vector<string>& v){
int count=0;
for(int i=0;i<v.size();i++){
if(is_Palindrome(v[i]))
count++;
}
return count;
}
4.
struct InventoryItem{
int _ID;
string _description;
int _onHand;
};
void display(InventoryItem item){
cout<<"Item Id: "<< item._ID<<endl;
cout<<"Item description: "<< item._description<<endl;
cout<<"Item Count: "<< item._onHand<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.