• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

lining up colums using printf

I would like for my output to look like an report even though I am printing strings. The strings can be of different lengths and I would still like them all to line up. Is there any way of doinf that with printf? I would like the output to look like this:

hagtey    hdhhr    hh
hshh       kdjdjjf   hfhfhfh
n            hf         hdhdhdhd

thanks
0
v_wall78
Asked:
v_wall78
  • 8
  • 4
  • 3
  • +2
1 Solution
 
InteractiveMindCommented:
I imagine that you'd need tab indents.
And I'm also guessing that you're using either C or PHP. With C at least, you can use the '\t' escape sequence to create a tab, e.g.

    printf( string1 + "\t" + string2 );

Regards;
0
 
InteractiveMindCommented:
But dependant on the length of each string, you may want to use a few tabs, ie:

    printf( string1 + "\t\t\t" + string2 );
0
 
v_wall78Author Commented:
This is the code that I have, and if I use tabs it cuts of the next string.


while ((p = getutxent()) != NULL) {

      if (p->ut_type==USER_PROCESS) {

        printf("%s   ", p->ut_user);   /* user login name*/
        if (full_name) {
          name = getpwnam(p->ut_user);
          printf("%s   ", name->pw_gecos);
        }
        printf("%s   ", p->ut_host);   /* host machine name*/
        printf("\n");
      }
    }

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
InteractiveMindCommented:
what exaclty do you mean by "it cuts of the next string" ?  Could you give an example of this please?
0
 
v_wall78Author Commented:
even thought the first string could be of different lengths I would still like for the second string to all start say for example in spot 15

marschke                dyn-169-226-101-203.milne-d.albany.edu
br539962                cpe-24-194-35-41.nycap.res.rr.com
ae885833                dw1-12.sciencelib.albany.edu
br539962                cpe-24-194-35-41.nycap.res.rr.com
jr9034          dw1-13.sciencelib.albany.edu
vw893136                cpe-24-194-6-121.nycap.res.rr.com
rs2814          h249-116.freedom.resnet.albany.edu
sds             co6g15v-mi320.milne-d.albany.edu
sk0907          h235-240.state.resnet.albany.edu
db7545          h161-65.empire-commons.resnet.albany.edu
dl9534          h218-30.colonial.resnet.albany.edu
br539962                cpe-24-194-35-41.nycap.res.rr.com
kp7123          cpe-69-204-176-18.nycap.res.rr.com
dk1055          dw1-16.sciencelib.albany.edu
rspencer                hp09923-mi323.milne-d.albany.edu
0
 
InteractiveMindCommented:
Right, what you're going to need to do, is something like this (in pseudocode):


         If   FirstString.length   <   8    set tabIndent = 3
  else If   FirstString.length   <   12   set tabIndent = 2
  ...
  for ( set i = 0; i < tabIndet; i++ )
    set tab += "\t"
  ...
  output: FirstString + tab + SecondString


Obviously, you'd use C, and make it a little smarter, but do you see what I'm getting at?

I know: tabs are bitches.  lol
0
 
v_wall78Author Commented:
yep, great
thanks
0
 
neteducationCommented:
uuaaahhh... does nobody know good old printf anymore ?

printf("%15s",username);

printf("%-15s",username);

will print it exactly 15 characters wide , right, respectivly left-aligned.
like this tables are easy to produce.
0
 
InteractiveMindCommented:
neteducation,
...you're the man. lol

I've never done much C -- so I too have learnt something new ;)
0
 
neteducationCommented:
some printf-implementations even support dynamic sizing during runtime like this:

int n=15;
printf("%*s",n,username);
0
 
InteractiveMindCommented:
hehe - now you're just showing off ;)
0
 
neteducationCommented:
admit... I didnt know this until 3 weeks ago... and my last C-Program is some 5 years old, or even more.
0
 
InteractiveMindCommented:
^_^
0
 
netcoolCommented:
Hi

you can implement a code in C with using string function to check using string.h

if the input variable of

int n;
char firststring[20],secondstring[40],finalstring[100];
 n  = strlen(firststring);
if(n<8)
 strcat(firststring,"\t\t\t");
if(n>8)
 strcat(firststring,"\t\t");
strcpy(finalstring,firststring);
strcat(finalstring,secondstring);
printf(finalstring);

try this code in c it will work

 
0
 
more2chanceCommented:
Ok. Your solution is here.
You said you already know the maximum length allowed for each column. So, what you need to do is pad each string with spaces so that it reaches this maximum length. Write a function to do the padding, with a string and a length as parameters, like so

//I suppose you have already included the strings //library!!!

string padded(string s, int maxLength){
  if(maxLength > strlen(s)){
    for(int i = strlen(s); i<maxLength; i++){
      s+=" ";   //attaches a space at end of string s
    }
  }
  return s;
}

//Call this function for each string to be displayed!:-)

cheers,
C:\more2chance\_

0
 
InteractiveMindCommented:
:-)
0
 
neteducationCommented:
thats what I though... why not using the 20 lines solution when there is a one line solution :-)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now