Question

I have the next exercise: Write a program to print all input lines that are longer than 10 characters.

I wrote this:

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line length */
#define NEED 10 //minimum lenght of a line to be printed

int main()
{
    int len;//length of last line
    char line[MAXLINE];//current line
    char lines[1000];//array with all lines
    int i;//current array zone for write
    i = 0;

    while ((len = getline(line, MAXLINE)) > 0)//get one line from user
        if (len > NEED){//if length of its is bigger than NEED
             i = copy(line, lines, i);//write character by character in lines
        }
    printf("%s", lines);//print lines

    return 0;
}

/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)//get the line
        s[i] = c;//character by character
    s[i] = '\n';//and add new line(\n) at last of its
    return i;//return length of line
}

// copy a character array to another and return last wrote zone of array
int copy(char from[], char to[], int i)
{
    int j=0;
    while(from[j] != '\0'){
        to[i]=from[j];
        i++;
        j++;
    }
    return i;
}

When I run this and I enter few lines bigger than 10 characters , program print lines and some more strange characters.I post a link to a photo to see. Why this is happening?

Screenshot

Was it helpful?

Solution

in getline() function,

 s[i] = '\n';

you added \n(newline character) at the end of string but string should end with \0(strings in C are terminated by this character) so use,

 s[i] = '\0';

OTHER TIPS

You have to remember that strings in C are terminated, there is an extra special character at the end of each string that acts as a terminator. If that character is not there all string-handling functions will continue until it finds the terminator, which may be quite far of. This leads to undefined behavior as the functions go if reading (and possibly writing) outside the string.

If you would have used the standard functions to read and copy strings it would be handled for you, but now you have to manually add the terminator character '\0'.

Also after copying the whole data to a new string(lines) at the end you have to append a '\0' character.

lines[i] = '\0'; 
printf("%s", lines);//print lines

Please, refer the "getline" function written below

int getline(char s[], int lim)      //Reads line into s and returns its length
{
int c,i;
                                //Performs error checking to not exceed allowed limit
for(i=0;i<lim-1 && ((c= getchar())!=EOF) && c!='\n';++i)
    s[i] = c;
if(c == '\n')                   //We add newline only if it's entered
{
    s[i] = c;
    ++i;
}
s[i] = '\0';                    //Puts null char to mark the end of string
return i;
}

We use

         if(c=='\n')

to ensure that new line is stored in array only if enter key is pressed and to differentiate it from EOF and exceeding max input size of 1000

Irrespective of any input, once we come out of "for" loop,

         s[i] = '\0'

have to be used to append this null character to string's end. This statement is used to ensure that the array knows when it ends so that in later part of program we can use it as a ref to find the end of array and its size

For your main program, instead of using "copy" function, the below code should be sufficient to print the output

if(len>MAXNEED)
       printf("Printed line - %s - ", lines);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top