USING JAVA! 3. Modify the stack program to issue a warning and return -1 if the
ID: 641288 • Letter: U
Question
USING JAVA!
3. Modify the stack program to issue a warning and return -1 if the client program attempt to pop an empty stack and give a warning if it tries to push onto a full one. Demonstrate the two cases in a main() client program.
7. We have a program that reads in a sequence of characters and prints them
in reverse order using a stack. Write a client program that allows the user to enter a
string and prints whether the string is a palindrome (read the same backwards) or not. Palindrome examples are "dad" and "racecar". Demonstrate the two cases in a main() client program.
8. Write a program that reads in a sequence of characters, and determines whether its parentheses, braces, curly braces, "<" and ">" are balanced. Hint: for left delimiters, push onto stack; for right delimiters, pop from stack and check whether popped element matches right delimiter. Demonstrate the two cases in a
main() client program.
Explanation / Answer
/***********************************************************************************/
7.
#include
#include
#include
void push(char c[],int length)
{
int i;
for(i=length;i>=1;i--)
cin>>c[i];
}
void pop(char c[],int length)
{
int i;
for(i=1;i<=length;i++)
cout< }
void palindrome(char c[],int length)
{
int i,j,count=0;
for(i=1,j=length;i<=length,j>=1;i++,j--)
{
if(c[i]==c[j])
count++;
}
if(count==length)
cout<<"Palindrome";
else
cout<<"Not Palindrome";
}
int main()
{
int n,i;
char c[25];
clrscr();
cout<<"Enter No of Characters:"< cin>>n;
push(c,n);
pop(c,n);
cout< palindrome(c,n);
getch();
return 0;
}
/***********************************************************************************/
8.
#include
#include
#include
void push(char c[],int length)
{
int i;
cout<<"Enter the character"< for(i=length;i>=1;i--)
cin>>c[i];
}
void pop(char c[],int length)
{
int i,count=0,rcount=0;
for(i=1;i<=length;i++)
{
if(c[i]=='('||c[i]=='{'||c[i]=='[')
count++;
if(c[i]==')'||c[i]=='}'||c[i]==']')
rcount++;
}
if(count==rcount)
cout<<"Operator Balanced";
else
cout<<"Operator Not Balanced";
}
int main()
{
int n,i;
char c[25];
clrscr();
cout<<"Enter No of Characters[size of stack]:"< cin>>n;
push(c,n);
pop(c,n);
getch();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.