Thread: String Justifying

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    String Justifying

    Hello people,

    I'm trying make right-justify the strings entered,
    User enters different size of strings and I'm calculating their length and find the maximum of them...
    For instance

    First Input
    -->str
    Second Input
    -->strin
    Third
    -->string

    as partial What I've done is below
    Code:
    s_max=strlen(string[0].surname);
    for(i=0;i<3;i++){
    	s_size = strlen(string[i].surname);
    	if( s_size > s_max){
    		s_max=(strlen(string[i].surname));
    	}
    }
    
    
    i=0;
    do{
    	printf("%s\n",string[i].surname);//it's the part that I cant solve
    	i++;
    	
    }while(i!=3);
    	
    	return 0;
    }
    the code above is a part of main func.,
    I want to print strings as Right Aligned and as I'm doing this, I want to use reference the longest string ,i.e. printf("%6s\n",...) printf("%Ns",...)

    What I cant do is that after I found the the longest string's length in for loop, how can I use that value while printing??
    %x s if s_max is 7 it's gonna bi %7s....

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You can use a star as a placeholder for a run time field width:
    Code:
    printf( "%*s\n", width, string );

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Noir View Post
    You can use a star as a placeholder for a run time field width:
    Code:
    printf( "%*s\n", width, string );
    Thanks noir, I coded excatly what you've written above and it works fine ...

    Sometimes it can be tough to reach this kind of information, because in beginner and regular C books does not contain such information, i.e. sams teach ur self C, I've had a look but couldnt find anything about this type tuning in issues..

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It can be helpful to supplement your instructional text with a IDEs help files or the man pages.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    28

    Smile

    Quote Originally Posted by citizen View Post
    It can be helpful to supplement your instructional text with a IDEs help files or the man pages.

    what do you mean by saying IDEs help files or man pages?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    man pages are help documentation for command line operating systems such as unix or linux. Since these operating systems come with compilers, it makes sence that there is also documentation that explains what certain functions do, what's declared in some standard header, et cetera. That's what compilers or integraded development environments come bundled with. Of course, you could always search for online documentation, like here, or try using any good search engine.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can easily solve this with a bit of arithmetic. Say the longest possible width is 10 char's wide, and you want something right justified. (Note that printf() has it's own justification parameter - but we're talking here about a do-it-yourself approach).
    So you subtract the number of char's (digits or letters) from that 10 and now you know how many tiimes you need to first print a space BEFORE you print your string, to make it perfectly right justified.

    Say your string is 6 char's wide: 10 - 6 equals 4, so:
    Code:
    for (i = 0; i < 4; i++)
        putchar(' ');
    
    or more generically:
    spaces = 10 - (strlen(yourString));
    for (i = 0; i < spaces; i ++) 
       putchar(' ');  /* or putchar(32);
    Just another way to solve the problem.

    Adak

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by citizen View Post
    man pages are help documentation for command line operating systems such as unix or linux. Since these operating systems come with compilers, it makes sence that there is also documentation that explains what certain functions do, what's declared in some standard header, et cetera. That's what compilers or integraded development environments come bundled with. Of course, you could always search for online documentation, like here, or try using any good search engine.
    Thanks for the explanation and I've had a look the site you suggested first, and looks like another world to me : ) but it must be really helpful for advanced implementations.

    Quote Originally Posted by Adak View Post
    You can easily solve this with a bit of arithmetic. Say the longest possible width is 10 char's wide, and you want something right justified. (Note that printf() has it's own justification parameter - but we're talking here about a do-it-yourself approach).
    So you subtract the number of char's (digits or letters) from that 10 and now you know how many tiimes you need to first print a space BEFORE you print your string, to make it perfectly right justified.

    Say your string is 6 char's wide: 10 - 6 equals 4, so:
    Code:
    for (i = 0; i < 4; i++)
        putchar(' ');
    
    or more generically:
    spaces = 10 - (strlen(yourString));
    for (i = 0; i < spaces; i ++) 
       putchar(' ');  /* or putchar(32);
    Just another way to solve the problem.

    Adak
    All I can say is , it's cool Though for a beginner programmer it's a bit hard to think of this way, it must be what the programming is .

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by m0ntana View Post
    Thanks for the explanation and I've had a look the site you suggested first, and looks like another world to me : ) but it must be really helpful for advanced implementations.



    All I can say is , it's cool Though for a beginner programmer it's a bit hard to think of this way, it must be what the programming is .
    I haven't taken the time to read through your other responses, but you DO know that, of course, printf() has a left and right justification flag, right?

    Naturally.

    Letters are left-justified, numbers right justified, by default, but that can be overridden.

    Try this:
    Code:
    int i = 2;
    char msg = {I want this right justified!\0};
    printf("\n&#37;+42s %d", msg, i);
    Excuse me, but I thought you already knew this or had received this answer from another poster already, and your instructor wanted you to do it "old school" style.

    Adak
    Last edited by Adak; 04-09-2007 at 02:51 PM.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Adak View Post
    I haven't taken the time to read through your other responses, but you DO know that, of course, printf() has a left and right justification flag, right?

    Naturally.

    Letters are left-justified, numbers right justified, by default, but that can be overridden.

    Try this:
    Code:
    int i = 2;
    char msg = {I want this right justified!\0};
    printf("\n%+2s %d", msg, i);
    Excuse me, but I thought you already knew this or had received this answer from another poster already, and your instructor wanted you to do it "old school" style.

    Adak
    Actually, I didnt know this, I mean printf("\n%+2s %d", msg, i); this part looks like a bit weird to me,and I've just compiled it and doesnt make any sense, maybe I just made something wrong.

    And yes I'm instructed to do it damn old school sytle, but I hate this..So far What I've understood from programming is that there's always second ways to do something, but old school type of programming makes you stop thinking or acting freely or more creative...

    From this kind of board and these people I've learned much more things than what I learned from instructors...

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey M0ntana, make it "%+42s, %d", not just %+2!!

    You'd have a heck of a hard time writing a 29 char string in a field just 2 char's long!

    Adak

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    28
    Quote Originally Posted by Adak View Post
    Hey M0ntana, make it "%+42s, %d", not just %+2!!

    You'd have a heck of a hard time writing a 29 char string in a field just 2 char's long!

    Adak
    Code:
    int i = 2;
    char *msg = {"I want this right justified!\n" };
    printf("\n%+50s %d", msg, i);}
    it works fine, obviusly I had tried it by puttin 14 but I didn't calculate the excat length of string as 29, that's why it didnt make any sense

    By the way , I said it looks like weird in my post above, because of "+" that you put in printf(),
    I just realized the both gives the same result (syntaxly same)
    &+50s and %50s, I put "-" but it doesnt return with any change...
    anyway thanks again, these things'r really fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM