patterncMinor
Print input lines longer than 80 characters
Viewed 0 times
longerthaninputprintcharacterslines
Problem
"The C Programming Language" (2nd Edition) by Kernighan and Richie has this exercise:
Write a program to print all input lines that are longer than 80 characters
I have written the following code for this exercise:
This code is shorter than many other suggestions I found on the Internet. The code I wrote seems to be working. Am I missing something, or can I suppose that this code is fine for the exercise?
Write a program to print all input lines that are longer than 80 characters
I have written the following code for this exercise:
#include
#define MAXLEN 1000
#define MINLEN 80
int main(void)
{
int c;
int index = 0;
char line[MAXLEN];
while ((c = getchar()) != EOF)
{
if (c != '\n')
{
line[index] = c;
++index;
}
else
{
line[index] = '\0';
if (index >= MINLEN)
{
printf("%s\n", line);
}
index = 0;
}
}
return 0;
}This code is shorter than many other suggestions I found on the Internet. The code I wrote seems to be working. Am I missing something, or can I suppose that this code is fine for the exercise?
Solution
Just to elaborate on @ratchet freak's answer, what you need is a two-state program.
In state 1, when fewer than 80 characters have been input on a single line, store these characters in a buffer and increment a counter to reflect the number of characters that have been stored. If you receive a newline character in this state, reset the counter and start again.
As soon as this counter reaches 80, print out the contents of this buffer and switch to state 2, where characters are output as soon as they are received. There is no need to continue buffering in this state. If you receive a newline character, go back to state 1.
With this approach, there's no need to store more than 80 characters in memory (plus one byte for the terminating
In state 1, when fewer than 80 characters have been input on a single line, store these characters in a buffer and increment a counter to reflect the number of characters that have been stored. If you receive a newline character in this state, reset the counter and start again.
As soon as this counter reaches 80, print out the contents of this buffer and switch to state 2, where characters are output as soon as they are received. There is no need to continue buffering in this state. If you receive a newline character, go back to state 1.
With this approach, there's no need to store more than 80 characters in memory (plus one byte for the terminating
'\0'), and no possibility of a buffer overflow.#include
#define MIN_LENGTH 80
int main(void) {
char buffer[MIN_LENGTH + 1];
int c, n=0;
while (1) {
c = getchar();
if (c == EOF) break;
if (n < MIN_LENGTH) {
/* State 1: Fewer than MIN_LENGTH characters in line */
if (c == '\n') n = 0;
else {
buffer[n++] = c;
if (n == MIN_LENGTH) {
buffer[n] = '\0';
printf("%s",buffer);
}
}
}
else {
/* State 2: At least MIN_LENGTH characters in line */
putchar(c);
if (c == '\n') n = 0;
}
}
return 0;
}Code Snippets
#include <stdio.h>
#define MIN_LENGTH 80
int main(void) {
char buffer[MIN_LENGTH + 1];
int c, n=0;
while (1) {
c = getchar();
if (c == EOF) break;
if (n < MIN_LENGTH) {
/* State 1: Fewer than MIN_LENGTH characters in line */
if (c == '\n') n = 0;
else {
buffer[n++] = c;
if (n == MIN_LENGTH) {
buffer[n] = '\0';
printf("%s",buffer);
}
}
}
else {
/* State 2: At least MIN_LENGTH characters in line */
putchar(c);
if (c == '\n') n = 0;
}
}
return 0;
}Context
StackExchange Code Review Q#79066, answer score: 7
Revisions (0)
No revisions yet.