Question

I am practicing using stack with c++. Objective of the program is to ask a user...

I am practicing using stack with c++. Objective of the program is to ask a user to input a series of letters, seperate it by $. See if they are symmetrical

So for instance, if abc$cba would make it work. Although if it were sabc$cba would be false because s is not symmetrical and does not match.

I am running into a bug where if I try to input letters that do not contain an $ and are in a group of odd numbers, such as 3 or 5, then the program will just bug out. ie "aaa" or "aaaaa." I need to find a solution that'll return false if the string does not contain an $ and is odd numbered amount of letters.

source: https://pastebin.com/LsimEmeC

header: https://pastebin.com/yrXndpDr

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

//In Language Problem

#include<iostream>
#include"Stack.h"
#include<string>
using namespace std;

bool checkIt_1(string s)
{
   bool b = true;
   Stack<char> myStack;
   int stringLength = s.length();
   int i;
   for (i = 0; i<stringLength; i++) {
       if (s.at(i) != '$')
           myStack.push(s.at(i));
       else
           break;
   }
   if (i == stringLength)
       b = false;
   else {
       while (!myStack.isEmpty() || i<stringLength - 1) {
           if (myStack.isEmpty() && i<stringLength - 1) {
               b = false;
               break;
           }
           else if (!myStack.isEmpty() && i == stringLength - 1) {
               b = false;
               break;
           }
           else if (myStack.peek() != s.at(++i)) {
               b = false;
               break;
           }
           else
               myStack.pop();
       }
   }
   return b;
}

bool checkIt_2(string s)
{
   Stack<char> myStack; // declare myStack as char
   int stringLength = s.length();
   if(stringLength % 2 == 1 && s.find('$') == -1) {
       return false;
   }
   if (stringLength == 0 || stringLength % 2 == 0) { // if length is even or has 0 characters, return false
       return false;
   }
   else {
       if (stringLength == 1) { //if character only has one length, and is $, return true. else false.
           if (s.at(0) == '$')
               return true;
           else
               return false;
       }
       else {
           //Pushing characters on the left side of $
           int i = 0;
           while (s.at(i) != '$' || (i < stringLength/2) && i<stringLength) { // as long as not $, push stack - checks length of string to keep it from going on forever
               //when looking for $
               myStack.push(s.at(i));
               i++;
           }
           // Now out of while loop,
           //either seen $ or reached the end of string with no $
           if (i == stringLength)
               return false;
           else {
               i++; //skips the $ in the string
               if (i == stringLength)
                   return false; //when the $ is at the end of the string
               else {
                   while (!myStack.isEmpty() && i != stringLength) {
                       if (myStack.peek() == s.at(i)) { //compares, if same, pop
                           myStack.pop();
                           i++;
                       }
                       else
                           return false;
                   }
                   //When we get out of while loop, either
                   //1. !myStack.isEmpty() has failed -> The stack is empty
                   //or
                   //2. i! stringlength has failed -> i==stringLength -> reached end of string
                   //or
                   //3. Both !myStack.isEmpty() and i==stringLength have failed -> Stack is empty and we reaached
                   //the end of the string.
                   if (myStack.isEmpty() && i == stringLength)
                       return true;
                   else
                       return false;
               }
           }
       }

   }
}

int main()
{
   string testString;
   cout << "Please enter a string:\n";
   cin >> testString;
   if (checkIt_2(testString))
       cout << "It is in the language!\n";
   else
       cout << "It is not in the language!\n";

   system("pause");
   return 0;
}

lease enter a string: It is not in the language! Press any key to continue . - . Process exited after 2.345 seconds with retu

Add a comment
Know the answer?
Add Answer to:
I am practicing using stack with c++. Objective of the program is to ask a user...
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
  • I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When...

    I am writing a program in c programming and it is supposed to do the following using mc9s12dg256.h microcontroller When both DIP switches #8 and #1 are high, turn on all LEDS. When both DIP switches #8 and #1 are low, turn off all LEDs When DIP switch #8 is high and #1 is low, turn on all the even numbered LEDs. When DIP switch #1 is high and #8 is low, turn on all the odd numbered LEDs. Your...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • I am having trouble with my C++ program.... Directions: In this lab assignment, you are to...

    I am having trouble with my C++ program.... Directions: In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node), but it need not be sorted. The IntegerSet class should have two data fields: the cardinality (size) of the set, and the head reference to the...

  • I am struggling with a program in C++. it involves using a struct and a class...

    I am struggling with a program in C++. it involves using a struct and a class to create meal arrays from a user. I've written my main okay. but the functions in the class are giving me issues with constructors deconstructors. Instructions A restaurant in the Milky way galaxy called “Green Alien” has several meals available on their electronic menu. For each food item in each meal, the menu lists the calories per gram and the number of grams per...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • I would like some assistance correcting an issue I am having with this assignment. Once a...

    I would like some assistance correcting an issue I am having with this assignment. Once a finite state automaton (FSA) is designed, its transition diagram can be translated in a straightforward manner into program code. However, this translation process is considerably tedious if the FSA is large and troublesome if the design is modified. The reason is that the transition information and mechanism are combined in the translation. To do it differently, we can design a general data structure such...

  • This is a java file and I am very confused on how to do this project!...

    This is a java file and I am very confused on how to do this project! please help!! Specifications Overview: You will write a program this week that is composed of three classes: the first class defines Ellipsoid objects, the second class defines EllipsoidList objects, and the third, Ellipsoid ListApp, reads in a file name entered by the user then reads the list name and Ellipsoid data from the file, creates Ellipsoid objects and stores them in an ArrayList of...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due...

    i need help with a mips program to to covert roman numerals to real numbers Lab 4: Roman Numeral Conversion Part A: Due Sunday, 19 May 2019, 11:59 PM Due Friday, 24 May 2019, 11:59 PM Part B: Minimum Submission Requirements Ensure that your Lab4 folder contains the following files (note the capitalization convention): o Diagram.pdf o Lab4. asm O README.txt Commit and push your repository Lab Objective In this lab, you will develop a more detailed understanding of how...

  • C++ Programming - Design Process I need to create a flow chart based on the program...

    C++ Programming - Design Process I need to create a flow chart based on the program I have created. I am very inexperienced with creating this design and your help will be much appreciated. My program takes a string of random letters and puts them them order. Our teacher gave us specific functions to use. Here is the code: //list of all the header files #include <iomanip> #include <iostream> #include <algorithm> #include <string> using namespace std; //Here are the Function...

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