You are on page 1of 4

1.

In a container of candies there are 5 colors randomly placed: red, yellow,


green, black and blue. Assume that the container holds a maximum of 25 candies.
Use a stack program to simulate randomly placing some (between 10 and 25 candies)
initially into the container (stack). this is interactive
Now once that is done have the "user" choose a color and using ONLY stack functions
remove all candies of that color. All candies in the container (stack) that are NOT of
that color are returned to the candy in the same order they were in prior to the color
removal. Actually loop the program twice to simulate "filling" and "removing" two
sets of candies. Don't assume that there are necessarily all colors of candy all the
time...so for example someone might want all red candies but there might not be
any red candies at that time.
Fill the container (or partially fill) only ONCE for each run.

Program:

#include<bits/stdc++.h>
using namespace std;

stack<char> remove(stack<char> container,char selectCandie){


char c;
stack<char> temp;
while(!container.empty()){
c=container.top();
if(c!=selectCandie)
temp.push(c);
container.pop();
}

while(!temp.empty()){
c=temp.top();
container.push(c);
temp.pop();
}

return container;
}

int main() {
char candie,selectCandie;
int n,i;
do{
cout << "Enter the number of candies between 10 and 25: ";
cin >> n;
}while(n<10 || n>25);
stack<char> container,temp;

for(i=0;i<n;i++){
cin >> candie;
container.push(candie);
}
cout << "Which candie you want : ";
cin >> selectCandie;
container=remove(container,selectCandie);
cout << "Now,remaining candies in container : ";
while(!container.empty()){
cout << container.top() << endl;
container.pop();
}
}

2. Write a program using stacks that tests whether or not a word, expression or
number is a palindrome. A palindrome is a word or expression that reads the same
forwards and backwards. (ignore punctuation and blanks) Test at least 10 phrases
and words. Be sure to label any palindromes as numeric or string in the output.
Make sure it works for more than just words and you MUST use stacks! Also if a
word is not a palindrome, see if the program can make it one….example: “hello” is
not a palindrome but “hellolleh” is!

Program:

#include <bits/stdc++.h>
using namespace std;

bool isPalindrome(stack<char> message) {


stack<char> temp;
int length = message.size();
int mid = length / 2;
char element,element1;

for (int i = 0; i < mid; i++) {


temp.push(message.top());
message.pop();
}

if (length % 2 != 0) {
message.pop();
}

while (!message.empty()) {
element = message.top();
element1 = temp.top();
message.pop();

if (element != element1)
return false;
temp.pop();
}
}

int main() {
string str;
stack<char> message,newMessage,temp;
cout << "Enter the string : ";
getline(cin,str);

for (int i = 0; i < str.length(); i++) {


if(isalpha(str[i]) || isdigit(str[i]))
message.push(str[i]);
}

if (isPalindrome(message))
cout << "\nString is a palindrome...";
else {
cout << "\nString is not palindrome but \"";

for (int i = 0; i < str.length(); i++) {


if(isalpha(str[i]) || isdigit(str[i]))
temp.push(str[i]);
newMessage.push(str[i]);
}

if(temp.size() % 2 != 0)
temp.pop();

while(!temp.empty()){
newMessage.push(temp.top());
temp.pop();
}
while(!newMessage.empty()){
cout << newMessage.top();
newMessage.pop();
}
cout << "\" is...";
}
return 0;
}

You might also like