Skip to content

Instantly share code, notes, and snippets.

@emsesc
Last active February 2, 2024 16:54
Show Gist options
  • Save emsesc/d8e88e6828a3358afde75a5c17d10d22 to your computer and use it in GitHub Desktop.
Save emsesc/d8e88e6828a3358afde75a5c17d10d22 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main() {
// initialize input array and int array holding offending lines
char lines[1000];
int offend[100] = {0};
// initialize counter for number of offending lines and the current lineNum
int oLines = 0;
int lineNum = 0;
// continuously receive lines of input until Null
while (fgets(lines, sizeof(lines), stdin) != NULL) {
// loop through chars in line
for (int i = 0; lines[i] != '\0'; i ++) {
// printf("Counter: %d, oLines: %d\n", i, oLines);
// if counter is at 51, 101, etc. newline prints
// will not print newline twice if the 51st char is a newline
if (i > 0 && i % 50 == 0 && lines[i] != '\n') {
printf("\n%c", lines[i]);
// special case: count line as offending
if (i == 50) {
offend[oLines] = lineNum;
oLines ++;
}
} else {
printf("%c", lines[i]);
}
}
lineNum ++;
}
printf("\nTotal lines over 50 chars: %d\n", oLines);
printf("Offending lines: ");
for (int i = 0; i < oLines; i ++) {
printf("%d, ", offend[i]);
}
printf("%c", '\n');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment