Know the row with max characters (C)
Posted
by
l_core
on Stack Overflow
See other posts from Stack Overflow
or by l_core
Published on 2012-06-22T21:11:38Z
Indexed on
2012/06/22
21:16 UTC
Read the original article
Hit count: 156
I have wrote 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?
Thank You Folks
© Stack Overflow or respective owner