0

I have written a program in C, to find the row with the max number of characters.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main (int argc, char *argv[])
{
    char c;                              /* used to store the character with getc */
    int c_tot = 0, c_rig = 0, c_max = 0; /* counters of characters*/
    int r_tot = 0;                       /* counters of rows */

    FILE *fptr;

    fptr = fopen(argv[1], "r");

    if (fptr == NULL || argc != 2)
    {
        printf ("Error opening the file %s\n'", argv[1]);
        exit(EXIT_FAILURE);
    }

    while ( (c = getc(fptr)) != EOF)
    {
        if (c != ' ' && c != '\n')
        {
            c_tot++;
            c_rig++;
        }

        if (c == '\n')
        {
            r_tot++;

            if (c_rig > c_max)
                c_max = c_rig;

            c_rig = 0;
        }
    }

    printf ("Total rows: %d\n", r_tot);          
    printf ("Total characters: %d\n", c_tot);
    printf ("Total characters in a row: %d\n", c_max);
    printf ("Average number of characters on a row: %d\n", (c_tot/r_tot));
    printf ("The row with max characters is: %s\n", ??????)

    return 0;
}

I can easily find the row with the highest number of characters but how can I print that out?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Lc0rE
  • 2,236
  • 8
  • 26
  • 34
  • I think it would be more correct to say "line" instead of "row" here. – Jay Sullivan Jun 22 '12 at 21:16
  • Just a note: in your while loop you are comparing the value of c against `'\n'` twice. You could avoid that by moving the row check first and using an else clause. Inside the else clause you would just check `if(c != ' ')`. – mlibby Jun 22 '12 at 21:21
  • c should be int, not char, since EOF is not a char value. – Jim Balter Jun 23 '12 at 00:21

2 Answers2

1

You'll need to store the line with the highest character count, e.g. in an array.

If you can make assumptions about line length, declare two arrays of characters:

char currentLine[255];
char maxLine[255];

Upon reading each character with getc, put it in the line array. After processing the line, if the current line has a higher count, copy the contents of currentLine into maxLine, using memcpy. You're already keeping track of the lengths for those two arrays as c_tot and c_max.

And if you can't make assumptions about line length, you can use the same technique but you'll need to malloc and realloc your buffers as you encounter lines longer than your initial size.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • How can i store the line if my "c = getc" is at the bottom of the row i need? – Lc0rE Jun 22 '12 at 21:15
  • You have c_rig to index the current array position. Then you make line[c_rig]=c; Don't forget to add the \0 at the end. – rlinden Jun 22 '12 at 21:18
0

You should store the current row as well in a char * and have a largest string char *. In the test where you determine the max size, you should also copy the current row to the largest string.

Don't forget to initialize the largest string to "" in order to avoid an error at the zero length file situation.

rlinden
  • 2,053
  • 1
  • 12
  • 13