Thread: Program not printing the first and last string characters

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    11

    Program not printing the first and last string characters

    Well I am almost done with this exercise where I had to print the longest line from a set of input lines.
    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    #define MAX 1000
    
    
    int main()
    {
    int len=0,i,j,c,k;
    char line[MAX];
    char longest[MAX];
    
    
    while ((c = getchar())!= EOF)
    {
        for(i=0,j=0 ; (c=getchar()) != '\n'; i++){
                line[i]=c;
                j++;}
        if(j>len){len = j;
                    for(k=0;k<j-1;k++){
                    longest[k]=line[k];
                    }
    }
    }
    printf("%d\n %s",len,longest);
    
    
    }
    There is just one problem. It leaves out the first and last character of the longest string. Where's my mistake?

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're throwing away the first char by reading it into c with the outer loop and then immediately reading another char into c with the inner loop.

    You're leaving out the last char by only copying up to j - 2 (by using the condition k < j - 1).

    Your j variable is useless since it mirrors i.

    You have no buffer-overflow protection.

    Your inner loop will become infinite if there's no newline after the last line in the file since you aren't testing for EOF there.

    You need to zero-terminate longest if you're going to print if with printf (or use any other standard library functions that require zero-terminated strings).

    And your spacing is terrible.

    BTW, are you not allowed to use fgets and strcpy?
    Code:
        while (fgets(line, sizeof line, stdin) != NULL))
            int sz = strlen(line);
            if (sz > len) {
                len = sz;
                strcpy(longest, line);
            }

  3. #3
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    I didn't see a statement like this before:
    Code:
    int len=0,i,j,c,k;
    What's it doing?
    I know this form:
    Code:
    int x=5;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int len=0,i,j,c,k;
    > What's it doing?
    Declaring 5 integer variables, but only initialising one one them to zero.
    Do you know which variable ends up being zero?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    12
    Quote Originally Posted by Salem View Post
    > int len=0,i,j,c,k;
    > What's it doing?
    Declaring 5 integer variables, but only initialising one one them to zero.
    Do you know which variable ends up being zero?
    I remembered it now, I think only len=0

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    11
    So how to write the inner loop without overwriting the initial char c? And how to zero-terminate a string?

    This example was given in K&R's first chapter and he never introduces strlen and strcpy in that so I thought there must be a way to do this without using them.

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Of course there's a way to do it without fgets, strlen and strcpy (you could, for example, write them all yourself!).

    But perhaps something like this:
    Code:
    i = len = 0;
    while ((c = getchar()) != EOF) {
        if (c == '\n') {
            if (i > len) {
                len = i;
                // copy line to longest
            }
            i = 0;
        }
        else
            line[i++] = c;
    }
    When copying line to longest don't leave off the last character! And remember to zero-terminate it, i.e., set the char after the last one you copied to '\0'. Surely the book must mention that. If the book hasn't mentioned that yet, then you can't print it out using printf (since printf wouldn't know where to stop printing characters), but you could print it with a loop that only prints the first len chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-09-2016, 02:59 AM
  2. printing last 2 characters of a long/string
    By rakabos in forum C Programming
    Replies: 3
    Last Post: 04-15-2010, 03:42 AM
  3. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  4. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  5. Printing with Unicode characters
    By chibisetsuna7 in forum C Programming
    Replies: 15
    Last Post: 06-19-2006, 10:26 PM

Tags for this Thread