Thread: Lining up my Array

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    3

    Lining up my Array

    Hello, I am a fresh newbie of C programming and I am having some trouble with a problem.

    When you execute this it prints it as: (assuming you use 'Name' as the first name and Lastname as the last name)

    Name LastName
    4 8


    The counts need to be aligned with the beginning of each name. The 4 under N and the 8 under the L.

    This is what I came up thus far.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(){
            char Name[10];
            char LastName[10];
    
            printf("What is your first name?: ");
            scanf("%s", Name);
            printf("What is your last name?: ");
            scanf("%s", LastName);
            printf("%s %s\n %d %d ", Name, LastName,
                   strlen(Name), strlen(LastName), sizeof Name, sizeof LastName);
    
            return 0;
    }
    Any sort of feedback is welcomed.

  2. #2
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    int name_len = strlen( Name );

    printf("&#37;s %s\n%d %*d ", Name, LastName, name_len, name_len, strlen(LastName) );
    Last edited by Aia; 03-18-2008 at 05:28 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    so &#37;*d aligns it?
    and why did you put strlen(Name) Twice?
    I guess I didn't have to put the sizeof functions either.

    sorry for the extra questions but I'd like to know why it works.

    Thank you Aia.

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    print allows you to format with defined fields.
    So %10d would display the decimal number in a minimum field of 10. As well you can use the * to define a variable minimum field of. Like %*d, variable field, actual value

    Some examples
    Last edited by Aia; 03-18-2008 at 05:47 PM.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    thanks for the info.

  6. #6
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    great code

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int main(){
            char Name[10];
            char LastName[10];
    
            printf("What is your first name?: ");
            scanf("%s", Name);
            printf("What is your last name?: ");
            scanf("%s", LastName);
            printf("%s %s\n %d %d ", Name, LastName,
                   strlen(Name), strlen(LastName), sizeof Name, sizeof LastName);
    
            return 0;
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your great code has several problems
    1. wrong number of parameters to printf
    2. potential buffer overrun on scanf

    and not so critical
    - missing fflush(stdout) between printf and scanf
    - using &#37;s format for reading strings - supposes that they do not have spaces
    - printing unsigned values with %d format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM