我在Ç編寫的程序行,找到行與字符的最大數量。知道,最大的字符(C)
下面是代碼:
#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;
}
我可以很容易地找到人物的最多的行,但我怎麼能打印說出來?
我認爲在這裏說「行」而不是「行」會更準確。 –
只是一個註釋:在你的while循環中,你將c的值與'\ n''的值比較兩次。您可以通過先移動行檢查並使用else子句來避免這種情況。在else子句中,你只需檢查'if(c!='')'。 – mlibby
c應該是int,而不是char,因爲EOF不是char值。 –