Ask Question
3 June, 08:27

Write a new program indent. cpp that enhances the program from the previous task. As it reads the input line by line, it should also count the number of open and closed { } in it, and keep track of how many blocks is currently open at the beginning of each line.

+5
Answers (1)
  1. 3 June, 10:07
    0
    see explaination

    Explanation:

    #include

    #include

    #include

    using namespace std;

    //Define the function to count a

    / / specific character in a line.

    int countChar (string line, char c)

    {

    //Define the variable.

    int tc = 0;

    //Iterate through the string.

    for (int i = 0; i < line. length (); i++)

    {

    //Find the matching character.

    if (line[i] = = c)

    tc++;

    }

    //Return the character counts.

    return tc;

    }

    //Define the function to remove the spaces.

    string removeSpace (string input)

    {

    //Find the string length.

    int strlength = input. length ();

    //Itearte through the string.

    for (int i = 0; i < strlength; i++) {

    //set the value.

    i = 0;

    //Check for the spaces.

    if (input[i] = = ' ') {

    //Remove the extra space.

    for (int j = 0; j < strlength; j++) {

    input[j] = input[j + 1];

    }

    //Update the string length.

    strlength--;

    }

    //if there is no extra space left.

    else

    break;

    }

    //Return the input string

    return input;

    }

    //Define the main function.

    int main () {

    //Define the variables.

    int n, ind = 0, ob = 0;

    string lines[100], input;

    int max = 0;

    //Prompt the user for input.

    cout << "Enter lines one by one: " << endl;

    //Get the user input and exit when

    / / user enters twice after the input.

    do

    {

    getline (cin, input);

    //Remove the extra space.

    input = removeSpace (input);

    //Store the line by line input in the

    //String array.

    if (input. length () > max)

    max = input. length ();

    lines[ind++]. append (input);

    } while (! input. empty () ! = 0);

    //Set the value for number of lines.

    n = ind;

    cout << endl;

    //Dsipaly the message

    cout << endl << "Indented code is:" << endl << endl;

    //Iterate through the string array.

    for (int i = 0; i < = n; i++)

    {

    string tab = "";

    //Check for the closing braces.

    int close = countChar (lines[i], '}');

    //Set the value for braces.

    ob - = close;

    //Indent the code.

    for (int j = 0; j < ob; j++)

    {

    tab + = " "; //it takes 4 whitespace

    //tab + = "/t"; //it takes 8 whitespace

    }

    //Print the line.

    cout << tab << lines[i]<< endl;

    //Check for the opening braces.

    int op = countChar (lines[i], '{');

    //Update the opening brace count.

    ob = ob + op;

    }

    //system ("pause");

    //Return the value 0.

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a new program indent. cpp that enhances the program from the previous task. As it reads the input line by line, it should also count ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers