Question

Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowerca...

Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). DO NOT USE THE STACK LIBRARY, QUEUE LIBRARY, OR ANY OTHER LIBRARIES NOT COVERED YET.

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

CODE:

#include<iostream>
#include<string>
#define MAX 100
using namespace std;
char S[MAX],Q[MAX];
int top=-1,front=-1,rear=-1;
void push(char ch)      //function for stack push operation
{
   top=top+1;
   S[top]=ch;
}
char pop()           //function for stack pop operation
{
   char ele;
   if(top==-1)
       return -1;
   else
   {
       ele=S[top];
       top=top-1;
       return ele;
   }
      
}
bool isEmpty()   //function for checking stack is empty or not
{
   if(top==-1)
   {
       return true;
   }
   else
   {
       return false;
   }
}
void enque(int x)   //function for queue enqueue operation
{
    if(rear==MAX-1)
        printf("QUEUE IS OVERFLOWED:");
    else
    {
        rear=rear+1;
        Q[rear]=x;
        if(rear==0)
            front=0;

    }

}
char deque()    //function for queue dequeue operation
{
   char ele;
    if(front==-1 || front>rear)
        printf("QUEUE IS UNDERFLOWED:");
    else
    {
       ele=Q[front];
        front=front+1;
        return ele;

    }

}
int main()
{
   string text,text1;
   int pal_length=0;
   char stack_element,queue_element;
   cout<<"Enter the text: ";
   getline(cin,text);
   text1=text;
   for(int i=0;i<text.length();i++) //logic for coverting string to lowercase
   {
       if(text[i]>='A' && text[i]<='Z')
       {
           text[i]=text[i]+32;
       }
   }
   for(int i=0;i<text.length();i++)
   {
       push(text[i]);         //placing each character of a string in stack and queue
       enque(text[i]);
   }
   while(!(isEmpty()))
   {  
       stack_element=pop();     //popping element from stack
       queue_element=deque();   //dequeuing element from queue
       if(stack_element==queue_element)   //comparing elements
       {
           pal_length=pal_length+1;
       }
   }
   if(pal_length==text.length())
   {
       cout<<"The text "<<text1<<" is a palindrome"<<endl;
   }
   else
   {
       cout<<"The text "<<text1<<" is not a palindrome"<<endl;
   }  
}

OUTPUT:

Enter the text: LeveL The text LeveL is a palindromeEnter the text: Stanley Yelnats The text Stanley Yelnats is a palindrome

Add a comment
Know the answer?
Add Answer to:
Write a simple program in C++ that reads a line of text, changes each uppercase letter to lowerca...
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
  • Write a program that reads a line of text, changes each uppercase letter to lowercase, and places...

    Write a program that reads a line of text, changes each uppercase letter to lowercase, and places each letter both in a queue and onto a stack. The program should then verify whether the line of text is a palindrome (a set of letters or numbers that is the same whether read forward or backward). please use c++ language and use own queue class or class provided. plese do not use queue library. please use functions.

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • This is a java question A palindrome is a word or phrase that reads the same...

    This is a java question A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and punctuations, and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: warts n straw radar Able was I ere I saw Elba tacocat Write a program named Palindrome.java that will accept a file (file name) from the command argument list and decide whether each line in the file is...

  • Need help in C (a) Write a C program to read in a line of text...

    Need help in C (a) Write a C program to read in a line of text and count the occurrence of each English alphabet. The lowercase version of a letter is considered the same as the uppercase. To make viewing easy, the frequencies should be presented using a bar chart as follows. You can assume that the input contains only spaces, lowercase letters, uppercase letters and the newline character (i.e. the Enter key). Enter a line of text: Letter ZZz...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

  • Write a program that reads a string from the keyboard and computes the two arrays of...

    Write a program that reads a string from the keyboard and computes the two arrays of integers upperCase, and lowerCase, each of size 26. The first one represents the frequency of upper case letters and the second one represents the frequency of lower case letters in the input string. After computing these arrays, the program prints the frequencies in the following format: The frequency of the letter A= The frequency of the letter B= Assume the input string contains only...

  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

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