Question

Q.1. Write a C program that determines whether a line entered by a user is a...

Q.1. Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation demonstrated in the class). Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out).
Q.2. Write a charQueue header file containing all the function headers of following functions:
1- initiateQueue—to initialize a queue 2- enqueue—to add a node at the rear end of the queue 3- dequeue—to remove a node from the front of the queue 4- printQueue—print a queue

and an implementation file implementing these functions for a Queue of chars. Inside your main method, which should be part of your tester file, read a sequence of chars one at a time. Terminate your sequence if user enters the number 0. Remember the difference between a char and an int!
Print the text user entered (that was stored in your Queue as a series of characters) using a while loop. You should loop as long as the Queue is not empty.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<string.h>
#define MAX 50
int top=-1;
char stack[MAX];
void push(char c)
{
   if(top==MAX-1)
   {
       printf("overflow");
   }
   else
   {
       top++;
       stack[top]=c;
   }
}
void pop()
{
   if(top==-1)
   {
       printf("underflow");
   }
   else
   {
       top--;
   }
}
int main()
{
   char str[50];
   int i,front=0;
   printf("enter the string:");
   scanf(" %[^\n]s",str);
   for(i=0;str[i]!='\0';i++)
   {
       push(str[i]);
   }
   for(i=0;i<(strlen(str))/2;i++)
   {
       if(stack[top]==str[i])
       {
           pop();
           front++;
       }
       else
       {
           printf("not palindrome");        //If not equal break the loop no need to check other characters.
           break;
       }
   }
   if(front==strlen(str)/2)
   {
       printf("palindrome");
   }
}

Explanation:

In this i used arrays to implement the stack.To check given string is palindrome or not ,it is enough to check the left half of the string with right half of the string.the top of the element in stack is compared with first element of the string like that we go ..

OUTPUT:

Dev-C++5.2.0.2 File Edit Search View Project Execute Debug Tools CVS Window Help globala) palin.cpp else 25 F 26 CAProgram Fi

Dev-C++5.2.0.2 File Edit Search View Project Execute Debug Tools CVS Window Help globala) palin.cpp else 25 F CAProgram Files

Q2.

Header File:

#include<stdio.h>
#define MAX 150
int front,rear;
char Q[MAX];
void initiateQueue() //To initialize the queue
{
   front=-1;
   rear=-1;
}
void Enque(char c) // inserting at rear end
{
   if(rear==MAX-1)
   {
       printf("Overflow");
   }
   else
   {
       rear++;
       Q[rear]=c;
       if(rear==0)
       {
           front=0;
       }
   }
}
void Dequeue() //deleting at front end
{
   if(front==-1||front>rear)
   {
       printf("Underflow");
   }
   else
   {
       printf("deleted character:%c\n\n",Q[front]);
       front++;
   }
}
void printQueue() //Displaying elements
{   int i;
   printf("Displaying queue elements:");
   for(i=front;i<=rear;i++)
   {
       printf("%c",Q[i]);
   }
}

Main.cpp

#include<stdio.h>
#include<string.h>
#include "charQueue.h"
int main()
{
   char str[150];
   int i=0;
   initiateQueue();
   printf("enter sequence of characters:\n");
   while(1)
   {
       scanf("%c",&str[i]);
       if(str[i]=='0')
       {
           break;
       }
       Enque(str[i]);
   }
   i=front;
   while(i<=rear)
   {
       printf("%c",Q[i]);
       i++;
   }
   printf("\n\n");
   Dequeue();
   printQueue();
}

OUTPUT:

Dev-C++5.2.0.2 File Edit Search View Project Execute Debug Tools CVS Window Help globala) palin.cpp charQueue.h main.cpp 4inc

I have done this code in Dev c++ IDE.

If any doubts comment below...

Add a comment
Know the answer?
Add Answer to:
Q.1. Write a C program that determines whether a line entered by a user is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming -- please use simple coding Write a C program that determines whether a line...

    C Programming -- please use simple coding Write a C program that determines whether a line entered by a user is a palindrome or not. You must demonstrate it as an application of stack (you may use linked list implementation).  Hint! Think of the basic property of a stack i.e. LIFO (list-in-first-out).

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • It is C++ problems, please explain your each line of code as well. Task 1: Implement/...

    It is C++ problems, please explain your each line of code as well. Task 1: Implement/ Hard Code a Doubly Linked Lists and make it a Stack. Do not use ::list:: class from the STD library for this example. The user will input a string and the program will output the string backwards. Displaying correct Stack implementation. Utilize the conventional static methods as needed. push() pop() empty() peek() peek() Sample Execution Please input String: happy yppah Task 2: Implement /...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • C language: Write a program that uses a function to check if a user entered string...

    C language: Write a program that uses a function to check if a user entered string is a palindrome. Based on the return value of the function, the results are printed in the main() function. (do not print results in function to check for palindrome) A palindrome reads the same forward as backward for example tacocat or civic.

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • 22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what...

    22.7 Lab: Word ladder Write a word ladder program. Read this wikipedia article that describes what a word ladder is: Word Ladder Your program must use a list to store the dictionary of words and then use stacks of strings and a queue of these stacks to solve for and output the word ladder. You are required to use the Standard Template Library (STL) list, stack, and queue. You must implement the WordLadder class. The class is declared in WordLadder.h....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT