0

I have recently started coding in language C and I was trying to do a question from a C programming book. The question was to print all lines that are longer than a specified length.

What I've done is take a line one by one and if the len>20 I append the line at the end of longest[] to get one final array that will store all the lines and will print the lines one by one.This is what I've achieved so far -->

#include <stdio.h>

#define LIMIT 2000
#define MAX 500

int getLine(char line[], int limit);
void copy(char line[], char longest[]);

int main()
{
    //Print lines>20 characters among other lines.
    int len,final=0;
    char line[MAX], longest[LIMIT];
    while((len=getLine(line,MAX)) > 0)
    {
        if(len>20)
        {
            copy(line,longest);
        }
    }
    while(longest[final] != '\0')
    {
        printf("%c",longest[final])
        ++final;
    }
    return 0;
}

int getLine(char line[], int limit)
{
    int ch, i=0;
    while(i<limit-1 && (ch=getchar())!=EOF && ch!='\n')
    {
        line[i]=ch;
        ++i;
    }
    if(ch=='\n')
    {
        line[i]=ch;
        ++i;
    }
    line[i]='\0';
    return i;
}

void copy(char line[], char longest[])
{
    int i=0;
    while(longest[i] != '\0' && i<LIMIT-1)
        ++i;

    int k=0;
    while(longest[i]=line[k] != '\0' && i<LIMIT-1)
    {
        ++i;
        ++k;
    }
    longest[i]='\0';
}

The issue I am facing is that when I am trying to print the final longest[] array which should be storing all the lines that are > 20 in length the final output is something like

LINES > 20 : M�[����ý

Any help would be appreciated, Thank you! :)

bot-coder101
  • 132
  • 1
  • 7
  • 3
    Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel May 30 '21 at 17:43
  • No I have not, I'll look into it right away. Thanks! – bot-coder101 May 30 '21 at 17:45
  • 2
    `longest[i]=line[k] != '\0'` needs some `(` `)` – KamilCuk May 30 '21 at 17:46
  • In function `copy()` you have `while(longest[i] != '\0' ...` When did you set it to `'\0'` in the first place? `char longest[LIMIT]` is uninitialised. – Weather Vane May 30 '21 at 17:48
  • Hi @WeatherVane, I really have no clue, how do I do it? can you please guide me a little in that direction? – bot-coder101 May 30 '21 at 17:54
  • 1
    `char longest[LIMIT] = "";` All the unused array elements are set to `0`. – Weather Vane May 30 '21 at 17:55
  • Thank you @WeatherVane the program works as intended now! :) – bot-coder101 May 30 '21 at 17:59
  • I hope you added the parentheses as advised: `while((longest[i]=line[k]) != '\0' && i – Weather Vane May 30 '21 at 18:00
  • Yes I did, I totally forgot that != has a higher precedence than = – bot-coder101 May 30 '21 at 18:01

0 Answers0