Question

C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std;...

C++ assignment. Please help me to complete this code:

#include <string>
#include <iostream>
using namespace std;

// Write your function here


//////////////// STUDENT TESTING ////////////////////
int run()
{
cout << "Student testing" << endl;
strip();
return 0;
}

Here is the instruction:

Write a filter function named strip that removes C++ comments from input, sending the uncommented portion of the program to output. Your program should work with both single line (//) comments, and multi-line (/* */) comments.

+ A "/*" sequence is ended by the first "*/" that is encountered.
+ Comment characters inside string literals "don't count"
+ Strings quotes may be escaped using an escape character.
+ Single-line comments // may be included inside multi-line comments
Because this is a filter, you program will read from cin and write to cout.

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

The program is implemented using two boolean variables "single" and "multi" which are used as flags and set to true when a comment is found in the input program.

The program works by taking the input (the source code that includes comments) in the string variable inp .The function strip() scans for the comment lines in the input.The relevant logic is set for ignoring the comment lines by finding whether the comment line is a single comment line or a multi line comment using the conditional logic to check for contiguous "//" symbols or "/*" with their corresponding closing symbols for single and multi line comments respectively.

If a comment is found,the comment line is skipped and the rest of statements will be storied in the output variable "res"(string datatype)

Execution:

The program prompts the user to provide the input(the program that inculdes comments) from the console.After the input is provided and enters a user-break character(Ctrl+D),the program provides the output by stripping all the comment lines from the output .

Source code:

commentstrip.cpp

#include <iostream>
using namespace std;
  
string strip(string inp)
{
int n = inp.length();
string res;
  
// Flag to to check existence of single comment line
bool single = false;
// Flag to to check existence of multi comment line
bool multi = false;
  
  
// scan the given input program code
for (int i=0; i<n; i++)
{
// If single line comment exists, then check for end of it
if (single == true && inp[i] == '\n')
single = false;
  
// If multiple line comment exists, then check for end of it
else if (multi == true && inp[i] == '*' && inp[i+1] == '/')
multi = false, i++;
  
// Ignore comment characters inside string literals
else if (single || multi)
continue;
  
// Check for beginning of comments and setting the flags
else if (inp[i] == '/' && inp[i+1] == '/')
single = true, i++;
else if (inp[i] == '/' && inp[i+1] == '*')
multi = true, i++;
  
// If current character is a non-comment character, append it to result
else res += inp[i];
}
return res;
}
  
int run(string inp)
{
cout << "\n Student testing" << endl;
cout << strip(inp);
}
// Driver program to test above functions
int main()
{
string inp,line;
cout<<"Enter the program to be stripped:"<<endl;
cout<<"Press Ctrl+D after providing the input:"<<endl;
while (getline(cin, line))
{
if (line == "^D")
break;

inp += "\n"+line;
}

cout << "Given Program \n";
cout << inp << endl;
run(inp);
return 0;
}

Output screenshots

:

Add a comment
Know the answer?
Add Answer to:
C++ assignment. Please help me to complete this code: #include <string> #include <iostream> using namespace std;...
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
  • Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence;...

    Here is the beginning of the code; #include <string> #include <iostream> int main() { std::string sentence; std::getline(std::cin, sentence); // manipulate the sentence here std::cout << sentence << "\n"; }} Lab 4.5.1 Text manipulation: duplicate white space Objectives Familiarize the student with: • the basics of string and text manipulation. Scenario Write a program that will read a line of text and remove all duplicate whitespace. Note that any single whitespace characters must remain intact. #include <string> #include <iostream> int main()...

  • #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...

    #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes here: int main() {     cout << "Welcome to Mad Lib.\n\n";     cout << "Answer the following questions to help create a new story.\n";     string name = askText("Please enter a name: ");          string noun = askText("Please enter a plural noun: ");          int number = askNumber("Please enter a number: ");          string bodyPart = askText("Please enter a body part: ");          string...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • #include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a...

    #include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }

  • #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index...

    #include <iostream> #include <sstream> #include <string> using namespace std; int main() {    const int index = 5;    int head = 0;    string s[index];    int flag = 1;    int choice;    while (flag)    {        cout << "\n1. Add an Item in the Chores List.";        cout << "\n2. How many Chores are in the list.";        cout << "\n3. Show the list of Chores.";        cout << "\n4. Delete an...

  • Find the problems with this program #include <iostream> using namespace std; struct Student { string name;...

    Find the problems with this program #include <iostream> using namespace std; struct Student { string name; int grade; } int main() { struct Student mary; Mary:name = "Mary"; Mary:grade = 100; cout << mary:name " got a " << mary:grade << endl;   return 0; }

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

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