0

This is a C program that should accept the terminal's input and return the longest line of the input alongside the length of that line. I know it's not as efficient as it could be made, but I'm trying to write with the few functions I know right now. In running it, it returns a segmentation error. An online debugger points out line 30 (which is flagged in the code below) but doesn't specify the problem. I'm not sure of it either, and I've been looking. What is the source of this error?

By the way, I know that there might be other errors. I want to find those myself. I only need help with that segmentation error.

#include <stdio.h>
#define MAX 200

int start = 0;
int i, j, k, x, finish;
int longlength;
char text[MAX];
char longest[MAX];

int main()
{
fgets (text, MAX, stdin);

for (i = start; text[i] != EOF; i++)
{
    if (text[i] == '\n')
    {
        finish = i - 1;
        break;
    }
}
for (j = start; j <= finish; j++)
{
    longest[j - start] = text[j];
}
longlength = finish - start;

for (k = finish + 1; (text[k] = '\n') && (text[k] != EOF); k++)
{
start = k;  //*****This is line 30*****
for (i = start; (text[(i + 1)] != '\n') && (text[(i + 1)] != EOF); i++)
{
}
finish = i;
if ((finish - start) > longlength)
{
    longlength = (finish - start);
    for (x = start; x <= finish; x++)
    {
        longest[(x - start)] = text[x];
    }
}
}
printf ("This is the longest line : %s.\n Its length is %d.", longest, longlength);
return 0;
}
11
  • 2
    Why do you check for EOF (end of file) and not for \0? Commented Aug 23, 2018 at 14:49
  • 1
    The fgets function writes a null-terminated byte string to the destination array. Also note that there are cases where fgets will not write the newline to the destination array, which means you could continue the program with finish equal to zero. You should also make sure the fgets call doesn't fail, which it indicates by returning a NULL pointer. Commented Aug 23, 2018 at 14:50
  • And regarding the code and the problem it's supposed to solve, there are much more simpler ways to handle it, if you're allowed to use the standard C functions like strcmp and strcpy. Commented Aug 23, 2018 at 14:53
  • What is on line 30? Commented Aug 23, 2018 at 14:58
  • 1
    for (k = finish + 1; (text[k] = '\n') && (text[k] != EOF); k++), In this loop text[k] is always assigned \n and checked against EOF. So the loop should run forever. Commented Aug 23, 2018 at 14:59

2 Answers 2

2

text[i] will never (or almost never) be EOF (which is usually defined to be -1), so your first loop won't terminate (unless the string contains a \n). Strings in C are null-terminated, and you should be checking for '\0', the null character.

You should try to run the code in a debugger, to see what's going on.

Sign up to request clarification or add additional context in comments.

Comments

0

Strings in C are typically terminated by \0 that's why you have to set that as loop termination.

In your case:

for (i = start; text[i] != '\0'; i++)
{
    //statements
}

Why do not use EOF:
You use it for Files, for instance .csv or normal .txt Files. Normal chararrays don't work with that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.