Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing multiple \t

Tags:

c

    switch(start)
   {case 0:printf("");
          j=1;
          break;
    case 1:printf("\t");
          j=2;
          break;
    case 2:printf("\t\t");
          j=3;
          break;
    case 3:printf("\t\t\t");
          j=4;
          break;
    case 4:printf("\t\t\t\t");
          j=5;
          break;
    case 5:printf("\t\t\t\t\t");
          j=6;
          break;
    case 6:printf("\t\t\t\t\t\t");
          j=7;
          break;
   }

start takes input from user, any way to shorten this piece of code??????? Any help is appreciated!!!!!!!!

like image 580
Oliver Queen Avatar asked Mar 17 '26 07:03

Oliver Queen


2 Answers

int foo(int start)
{
    for(int x = 0; x  < start; x++) printf("\t");
    return start + 1; // it is your j
}

or without the function

for(int x = 0; x  < start; x++) printf("\t");
j = start + 1; 
like image 64
0___________ Avatar answered Mar 19 '26 22:03

0___________


You could use start to compute the place in a buffer full of tabs at which to start printing:

if ( 0 <= start && start <= 6)
{
char* tabs = "\t\t\t\t\t\t"; // 6 tabs
  printf( "%s", tabs+6-start);
  j = start + 1;
}
like image 22
dmuir Avatar answered Mar 19 '26 21:03

dmuir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!