Thread: C programming help please

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    1

    Question C programming help please

    Hi i have the code below but i need to be able to write more code so it can recognise the longest name and print it can any one help please

    This is what i have
    Code:
    #include <stdio.h>
    #include <string.h>
    #define size 10
    main()
    
    {
    
    char name [10][25];
    
    int i;
    
    for (i=0;i<10;i=i+1)
    
    {
         printf("\nPlease enter name %d :",(i+1));
    
         gets(name[i]);
    }
         printf("%d",strlen(name[0]));
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    int length, longest = 0, longest_idx= 0; //idx = index
    
    //inside your for for loop, add:
    
    length = strlen(name[i]);
    
    if(length > longest) {
        longest = length;
        longest_idx = i;
    }
    
    printf("\n the longest name is: %s, which has %d letters", name[longest_idx], longest);
    Try putting that kind of logic, into your program.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Adam... Do you really not know how to do this?

    Look at the code you've got... arrays, indexes, loops, input, output...
    If you actually wrote that code you should easily be able to work out the rest.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Egad! While not related to your longest string issue, the following should be addressed:

    Adak's got you on the right path though.

Popular pages Recent additions subscribe to a feed