Professional Documents
Culture Documents
C++ Test
C++ Test
Program:
#include<bits/stdc++.h>
using namespace std;
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;
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);
if (isPalindrome(message))
cout << "\nString is a palindrome...";
else {
cout << "\nString is not palindrome but \"";
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;
}