1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
In-s [12.5K]
3 years ago
10

Write a new program indent.cpp that enhances the program from the previous task. As it reads the input line by line, it should a

lso 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.
Computers and Technology
1 answer:
Alborosie3 years ago
6 0

Answer:

see explaination

Explanation:

#include <iostream>

#include <conio.h>

#include <string>

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;

}

You might be interested in
What are TWO examples of soft skills?
Elanso [62]
Computer programming and throubleshooting
3 0
3 years ago
Which tool allows multiple users to dynamically update a document at the same time? Question 3 options: Google Docs OneDrive Exc
GenaCL600 [577]

Answer:

Excel

Explanation:

7 0
2 years ago
If you wanted to detect x-rays coming from the sun,where would you place the detector? Why?
Temka [501]
You would place it in orbit around Earth, as most X-rays are absorbed by the atmosphere, so you would want the detector outside of the atmosphere to accurately detect the X-rays.
5 0
3 years ago
Is the computer a device that calculates and has the independence and initiative for the actions it performs?
inna [77]

Answer:

The answer is "True".

Explanation:

In this question, the given statement is true because the computer is a device that receives information as input, analyzes information utilizing a program that provides relevant information for the processed data, and in this, it performs numerous calculation and all the calculation will be store in its memory, which is used in the future for collect data on hard drives.

8 0
3 years ago
3. How can you correct the color of your photos automatically?​
natita [175]

Answer:

use the lightroom

download the app haha

4 0
3 years ago
Read 2 more answers
Other questions:
  • A set of communication rules for the computer to follow is called, what?
    10·2 answers
  • Do we have to use intersection?
    6·1 answer
  • Whenever you are passing or another vehicle is passing you, _______ is particularly important to avoid collisions. A. Turning. B
    9·2 answers
  • What are some consequences of internet addiction​
    9·1 answer
  • Security and protection as it relates to operating systems is grouped into four categories: Availability, Data integrity, Authen
    9·1 answer
  • What is a boolean in Java
    5·1 answer
  • T F Changes to a function parameter always affect the original argument as well.
    15·1 answer
  • An organization is developing an authentication service for use at the entry and exit ports of country borders. The service will
    10·1 answer
  • X = 9 % 2if (x == 1):  print ("ONE")else:  print ("TWO")
    13·1 answer
  • A text that presents an event and describe what happens as a result is an example of what text structure
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!