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]
4 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:
Alborosie4 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
True or false? Software application packages function as the interface between the operating system and firmware.
olga2289 [7]

Answer:

h

Explanation:

8 0
3 years ago
Time
weeeeeb [17]

Answer:

Time

TARDIS

Gel electrophores

outile

here exterior and

side

ower Resembles even with more

common that there size what

Time

า (

esem

e they

end interior of

4 0
3 years ago
Jacob is a website designer. Whenever his company takes on a new project, Jacob takes the initiative and comes up with ideas and
Troyanec [42]
Eating a potato is what hes using
3 0
3 years ago
I'm taking computer science and we are using a software called Python, i never used it before and the assignment is hard for me
Anarel [89]
Python has a help page on google I would definitely recommend that. Here is a link to the beginners guide https://wiki.python.org/moin/BeginnersGuide
3 0
3 years ago
The right to make others do things is referred to as _________.
Nady [450]

Answer:

I think B hope its right

3 0
3 years ago
Read 2 more answers
Other questions:
  • On an Android device, where can a user find the correct app to use to sync contacts and apps among devices?
    10·1 answer
  • Give one example of a civil engineering structure.
    5·1 answer
  • What will the following segment of code output if 11 is entered at the keyboard?
    12·1 answer
  • Jemima is reviewing her history report and notices that her headings are not formatted the same throughout the
    7·1 answer
  • Which of the items listed is not considered to be personal protective equipment (PPE)?
    14·1 answer
  • What should be included in research for a problem statement? Select all that apply
    13·2 answers
  • True or False: Mapping annotations are exclusive - an annotated method will only be accessible to requests sent to a matching UR
    14·1 answer
  • An analogue sensor has a bandwidth which extends from very low frequencies up to a maximum of 14.5 kHz. Using the Sampling Theor
    9·2 answers
  • What are software applications?
    14·1 answer
  • Consider the following code and its output:
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!