Question
c++
1) String format checker A string is referred to as ABA if it contains n > 0 consecutive As followed by n consecutive Bs f
8) The program reads one string at a time. Finds its length and determines its format compliance It should read the input dat
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here i am providing the answer. Hope it helps, please give me a like. it helps me a lot.

#include<iostream>
#include<fstream>

using namespace std;

//Queue class
class Queue
{
private:
//data members
int front, rear, count, capacity;
char *q;
public:
//constructor
Queue();
//public functions
void enqueue(char c);
char dequeue();
char getFront();
bool isEmpty();
void resize();
void reset();
};
//constructor
Queue::Queue()
{
capacity = 20;
q = new char[capacity];
front=rear=-1;
count=0;
}
//function enqueue
void Queue::enqueue(char c)
{
if(count==capacity)
resize();
rear = (rear+1)%capacity;
q[rear] = c;
count++;
}
//function dequeue
char Queue::dequeue()
{
if(isEmpty())
return ' ';
front = (front+1)%capacity;
count--;
return q[front];
}
//function return front value
char Queue::getFront()
{
if(isEmpty())
return ' ';
int tfront = (front+1)%capacity;
return q[tfront];
}
//function resize the queue
void Queue::resize()
{
int n = capacity;
capacity = capacity*2;
char *tmp = new char[capacity];
for(int i=0; i<n; i++)
{
tmp[i] = q[i];
}
delete []q;
q = tmp;
delete []tmp;
}
//function reset the queue
void Queue::reset()
{
count = 0;
front = rear = -1;
}
//function isEmpty return true when queue is empty
bool Queue::isEmpty()
{
return count==0;
}

//function returns true when a string format is valid otherwise false
bool isValid(string s, Queue q)
{
//enqueue all the characters of the string
for(int i=0; i<s.size(); i++){
q.enqueue(s[i]);
}

int n=0;
//count number of A at beginning
while(!q.isEmpty() && q.getFront()=='A')
{
q.dequeue();
n++;
}
//return false if the string is invalid
if(q.isEmpty()) return false;

int m = n;
//check n number of B in the middle
while(!q.isEmpty() && q.getFront()=='B')
{
q.dequeue();
n--;
}
//return false if the string is invalid
if(q.isEmpty() || n!=0) return false;
//check n number of A at end
while(!q.isEmpty() && q.getFront()=='A')
{
q.dequeue();
m--;
}
//return true if the string is valid
if(q.isEmpty() && m==0) return true;
//return false if the string is invalid
return false;
}

//main function
int main(int argc, char *argv[])
{
ifstream fin;
//check the number of arguments
if(argc<2)
{
cerr<<"Too few arguments!"<<endl;
return 1;
}
//open the file
fin.open(argv[1]);
//check file exist or not
if(!fin.is_open())
{
cerr<<"File not exist! "<<endl;
return 1;
}

Queue q;
string str;
while(1)
{
//read a string from the file
fin>>str;
//check end of file
if(fin.eof()) break;
//check the string format is valid or not
if(isValid(str, q)== true)
cout <<str << " == Format is OK"<<endl;
else
cout <<str << " == Format not acceptable"<<endl;
}
return 0;
}

exp.dat (input file)

AAABBBAAA
aaaa
ABAABBBBAAAA
BBBB

Output:

g++ stringFormatChecker.cpp

a exp.dat
AAABBBAAA == Format is OK
aaaa == Format not acceptable
ABAABBBBAAAA == Format not acceptable
BBBB == Format not acceptable

Thank you. please like.

Add a comment
Know the answer?
Add Answer to:
c++ 1) String format checker A string is referred to as A"B"A" if it contains n...
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++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header...

    IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header and implementation file to define an apartment class. Create a driver program to test the class, and create a make file to compile the driver program. Create two files called apartment.h and appartmentImp.cpp along with creating a driver program named testApartment.cpp containing the main function. Program Requirements: • Class attributes should include integers for number of rooms, monthly rent, and square feet, as well...

  • Consider the following C++ program. It reads a sequence of strings from the user and uses...

    Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13 +...

  • Instructions: Consider the following C++ program. It reads a sequence of strings from the user and...

    Instructions: Consider the following C++ program. It reads a sequence of strings from the user and uses "rot13" encryption to generate output strings. Rot13 is an example of the "Caesar cipher" developed 2000 years ago by the Romans. Each letter is rotated 13 places forward to encrypt or decrypt a message. For more information see the rot13 wiki page. #include <iostream> #include <string> using namespace std; char rot13(char ch) { if ((ch >= 'a') && (ch <= 'z')) return char((13...

  • Syntax checker. We will write C code to check to see if an input string can be derived from a gra...

    Syntax checker. We will write C code to check to see if an input string can be derived from a grammar. The grammar is: A ::= 0 B B ::= 1 A | 2 B | ; Your program "check.c" should output "yes" if the line of standard input can be derived from this grammar, and "no" otherwise. Here are some examples: $ ./check 0 ; yes $ ./check 0 2 1 ; no $ The input values will always...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • In C++, You are given a file of customer data with records in the following format...

    In C++, You are given a file of customer data with records in the following format : account number, last name, first name, middle name. Write a program to convert the input data and display it to the monitor in the following format : Accout Name     Logon ID Initial Pswd 21-88282712-B Keith S. Adams ADAM21 Adam88282 08-36847734-A John R. Sturm STUR08 Stur36847    etc. Notes :     1) The account number on the input is 10 digits     2)  The names on...

  • 1. You are given a C file which contains a partially completed program. Follow the instructions...

    1. You are given a C file which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

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