Reputation: 91
How can I control the tab position in order to properly indent a piece of code given as input?
I use a counter for the brackets to set the proper amount of indentation (e.g. if the first {
is in position 1 then the next should be in position 9).
Here is my code:
#include <stdio.h>
#include <stdlib.h>
main() {
int ch;
ch = getchar();
int lbcounter = 0; // Left curly bracket counter
int rbcounter = 0; // Right curly bracket counter
while (ch != EOF) {
if (ch != ';' && ch != '{' && ch!= '}') {
putchar(ch);
}
if (ch == ';') {
putchar(ch);
putchar('\n');
}
if (ch == '{') {
putchar('\n');
if(lbcounter == 0) {
putchar('\n');
lbcounter++;
putchar(ch);
putchar('\n');
} else {
putchar('\t');
lbcounter++;
putchar(ch);
putchar('\n');
}
}
if (ch == '}') {
putchar('\n');
if(rbcounter == 0) {
putchar('\t');
rbcounter++;
putchar(ch);
putchar('\n');
} else {
putchar('\n');
rbcounter++;
putchar(ch);
putchar('\n');
}
}
ch = getchar();
}
}
For example, for the following input:
int main(void){{{{{{{;;(void)((((0))));;return 0;}}}}}}}
we expect the following output:
int main(void)
{
{
{
{
{
{
{
;
;
(void)((((0))));
;
return 0;
}
}
}
}
}
}
}
but the actual output is:
int main(void)
{
{
{
{
{
{
{
;
;
(void)((((0))));
;
return 0;
}
}
}
}
}
}
}
To sum up: each time a left curly bracket is read from the input, I want to increase the indentation by one \t
— unless it is the first one, in which case I just want to insert a newline.
When the first right curly bracket is read, I want it to match the position of the last left bracket. Each time a new right bracket is read, the indentation should then decrease by one \t
.
Upvotes: 2
Views: 160
Reputation: 844
I have reviewed your code and you have got fundamental programming logic problems I think you should practice programming such algorithms more in order to promote your programming skill and your algorithmic thinking ability.
I have written a code that will do the job for you But you have to always remember that programming is sometimes a very long process of coding and trying different inputs and then analysing the outputs.
And in some cases debugging a written code is more important than the coding itself.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch;
ch = getchar();
int prevch=ch;
int lbcounter=0; /*Left curly bracket counter*/
int rbcounter=0; /*Right curly bracket counter*/
while (ch != EOF)
{
if (ch != ';' && ch != '{' && ch!= '}')
{
if(prevch=='\n')
for(int i=lbcounter;i>0;i--)
{
putchar('\t');
}
putchar(ch);
prevch=ch;
}
if (ch == ';')
{
if (prevch == ';' || prevch == '{' || prevch== '}'||prevch=='\n')
for(int i=lbcounter;i>0;i--)
{
putchar('\t');
}
putchar(ch);
putchar('\n');
prevch='\n';
}
if (ch == '{')
{
if(lbcounter==0)
{
putchar('\n');
lbcounter++;
putchar(ch);
putchar('\n');
prevch='\n';
}
else
{
for(int i=lbcounter ;i>0;i--)
{
putchar('\t');
}
putchar(ch);
lbcounter++;
putchar('\n');
prevch='\n';
}
}
if (ch == '}')
{
lbcounter--;
for(int i=lbcounter;i>0;i--)
{
putchar('\t');
}
putchar(ch);
putchar('\n');
prevch='\n';
}
ch=getchar();
}
}
And as the last advise try to understand the code instead of just using it.
The logic for the first if block: You have to take into concern that for characters except ';' , '}' , '{' there should the the same number of tabs before the characters to put the characters in the same indentation level of the block that was created by the last '{' character , and if only the previous character was the '\n' there must be tabs because for a string like 'void' you shouldn't put tabs between two characters.
The logic of the second if block: This if blocks does the same thing as the first if block with two differences First: it puts a new line character after the ';' character and Second : puts the required tab only if the previous character that was put is one of the following ';','}','{','\n' , because in a string like 'void;' the tabs shouldn't be placed before the ';' character.
The logic of the third if block: You know that the a '{' should be followed by a '}' character with the same indentation level so you have saved the indentation level of the last '{' character in the 'lbcounter' so you put the same number of tabs just before the '}' and after each '}' the indentation level decreases by one 'labcounter--'.
Upvotes: 0
Reputation: 1497
The problem is that indentation is not preserved when you insert a newline character. What you need is a variable that stores the current number of tabs.
int tab_qty = 0; // before your while loop
tab_qty++; // after printing a left bracket
tab_qty--; // before printing a right bracket
Then you insert \t
N times (where N = tab_qty
) at the beginning of lines.
Upvotes: 1
Reputation: 40145
#include <stdio.h>
int main(){
int ch;
int bcounter=0; /* bracket counter */
int topOfLine = 1;
while((ch=getchar()) != EOF){
if(ch == '{' || ch == '}'){
if(!topOfLine)
putchar('\n');
topOfLine = 1;
}
if(ch == '}'){
--bcounter;
}
if(0 < bcounter && topOfLine){
int i;
for(i = 0; i < bcounter; ++i)
putchar('\t');
}
putchar(ch);
topOfLine = 0;
if(ch == '{' || ch == ';'){
putchar('\n');
topOfLine = 1;
}
if(ch == '{'){
++bcounter;
}
}
}
Upvotes: 2