Replacing multiple \t

Viewed 252
    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!!!!!!!!

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; 

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;
}
Related