A
Army1987
Is this OK? If not, why?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 32767
#define DIGITS 5 /* number of decimal digits in MAXLEN */
int main(void)
{
size_t i;
int j;
char word[MAXLEN];
char format[DIGITS+3];
int counts[MAXLEN];
size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
word[MAXLEN - 1] = '\0';
sprintf(format, "%%%ds", MAXLEN);
while (scanf(format, word) > 0) {
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
scanf("%*s");
}
len_curr = strlen(word);
counts[len_curr]++;
if (len_curr > max_curr)
max_curr = len_curr;
}
for (i=1; i<=max_curr; i++) {
printf("%*d|", DIGITS, (int)i);
for (j=0; j<counts; j++)
putchar('=');
putchar('\n');
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 32767
#define DIGITS 5 /* number of decimal digits in MAXLEN */
int main(void)
{
size_t i;
int j;
char word[MAXLEN];
char format[DIGITS+3];
int counts[MAXLEN];
size_t len_curr, max_curr = 0;
memset(counts, 0, MAXLEN * sizeof(int));
word[MAXLEN - 1] = '\0';
sprintf(format, "%%%ds", MAXLEN);
while (scanf(format, word) > 0) {
if (word[MAXLEN-1] != '\0') {
word[MAXLEN-1] = '\0';
fprintf(stderr, "Truncating word longer than %d "
"characters to %s.\n", MAXLEN-1, word);
scanf("%*s");
}
len_curr = strlen(word);
counts[len_curr]++;
if (len_curr > max_curr)
max_curr = len_curr;
}
for (i=1; i<=max_curr; i++) {
printf("%*d|", DIGITS, (int)i);
for (j=0; j<counts; j++)
putchar('=');
putchar('\n');
}
return 0;
}