File program which gives output induntation code when we call that file actualy it was uninduntationed before

Votes + Comments
Do not mass post without showing honest effort
5 Years
Discussion Span
Last Post by WaltP

Daniweb is not a homework service. Please make an honest attempt to solve the problem yourself, then ask specific questions if you need help.

Edited by deceptikon


As a piece of advice, I can suggest this: assuming that the language you are indenting is C (or one with a similar block syntax, such as Java or C++), then the simplest solution is:

set the indent amount to four
set the indentation level to zero
for the length of the file:
    read the next char
    if the char is a newline:
        insert the newline
        insert (indent amount * indentation level) spaces
    else if the char is a whitespace:
        discard it; 
    else if the char is an open brace:
        insert a newline
        insert the open brace
        increment the indent level by one
        insert a newline
        insert (indent amount * indentation level) spaces
    else if the char is a close brace:
        insert a newline
        decrement the indent level by one
        insert close brace
        insert a newline
        insert (indent amount * indentation level) spaces
        insert the char as is

if indentation level is still more than zero, emit an error message

Now, this is just a rough method, and doesn't properly handle some edge cases (e.g., struct definitions), but it should be enough to get started with.

Edited by Schol-R-LEA


i tried it . . . In condition i dont no how to put 1st flower braces in 1st and next flower braces 4 space after


flower braces?

() these are called parentheses -- or parens to those in the business. (we're lazy)
[] these are square brackets, or for us lazies, just brackets
{} these are curly brackets/braces, know simply as braces in the industry.

Edited by WaltP

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.