you will write some simple programs (on paper) that shows that you have understo
ID: 3620939 • Letter: Y
Question
you will write some simple programs (on paper) that shows that you have understood the programming aspects using STL routines forstring,
stack
queue
list
//////////////////////////////////////////////////////////////////////////////////////
you will write ONLY the functions
below I give descriptions so you’ll understand what I mean
//////////////////////////////////////////////////////////////////////////////////////
////////////////////
sample string function
Given 2 strings s1 and s2 write a function to concatenate the 2 strings (in that order)
string fn(string s, string t) {
string a;
a=s+t;
return a;
}
e.g. in the main program if I have
string Lincoln”, another=”was president”, answer;
answer = fn (onestring, another);
// the above should make answer==”abe Lincolnwas president”
You can write the function above and the main program and test it yourself
/////////////
//////////////////
Sample stack function
Display top to bottom only those stack entries that are 0 or negative in a stack of integers
void negative(STACK s) {
while (!s.empty()) {
if (s.top() <=0) {
cout << s.top() << endl;
s.pop();
}
}
}
///////////////////////
///////////////
Sample queue function
Display front to back only those queue entries that are 0 or positive in a queue of integers
void positive(QUEUE s) {
while (!s.empty()) {
if (s.front() >=0) {
cout << s.front() << endl;
s.pop();
}
}
}
///////////////////////
///////////////
Sample list function
Given two list of integers L1 and L2 (containing the same number of items in them) create a new list L that will contain the “intertwined” items in the two given lists;
e.g., if L1= 2 4 6 9 10 and L2= 1 3 7 5 12 (both with 5 numbers in them) then
L=2 1 4 3 6 7 9 5 10 12
LIST intertwine(LIST L1, LIST L2) { //in the below IT denotes forward iterator
LIST temp;
If (L1.empty()) return temp; //empty lists L1 and L2
IT it1=L1.begin(), it2=L2.begin();
temp.push_back(*it1); temp.push_back(*it2); it1++; it2++;
while (it1!=L1.end()) {
temp.push_back(*it1);
temp.push_back(*it2);
it1++;
it2++;
}
return temp;
}
///////////////////////
/////////////////////////////////////
NOW THE HW2 PROBLEMS
Q1
Given a string s and a “pattern” string p write a function that locates all occurrences of the pattern string p in the given string s and then deletes them from s. call your function Q1.
e.g.
string source=”hello how is he doing and she is doing?”
string pattern=”he”;
Q1(source, pattern);
//the above should make source==”llo how is doing and s is doing?”
Q2
Given a string s write a function to remove all the vowels from it (vowels are: a e i o u - both upper and lower cases)
Call your function Q2
e.g.
string source=”hello how is he doing And shE is doing?”
Q2(source);
//the above should make source=”hll hw s he dng nd sh s dng?”
Q3
Given a stack s of integers write a function to print its contents from the bottom to top
Call your function Q3
e.g.
if the stack s contents are (top to bottom) 12 15 32 4 -23 5
Q3(s);
//the above should display on the screen: 5 -23 4 32 15 12
Explanation / Answer
Dear,
2.
string Q2(string source)
{
string str=source;
int k=0;
for(int i=0;i<str.length();i++)
{
if(tolower(str[i])=='a'||tolower(str[i])=='e'||tolower(str[i])=='i'||tolower(str[i])=='o'||tolower(str[i])=='u')
{
for(int j=i;j<str.length();j++)
str[j]=str[j+1];
str[j]='';
}
}
return str;
}
3.
class Stack
{
private:
int *stackArray; // Pointer to the stack array
int stackSize; // The stack size
int top; // Indicates the top of the stack
public:
// Constructor
IntStack(int);
// Copy constructor
IntStack(const IntStack &);
// Destructor
~IntStack();
// Stack operations
void push(int);
void pop(int &);
bool isFull() const;
bool isEmpty() const;
};
If the class looks like this the the function
Q3(Stack s) is
Q3(Stack s)
{
for(int i=0;icout<
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.