Only print input lines longer than 10 characters (ANSI C) - c

So I'm writing a practice program in C which has the purpose of taking user input and then after EOF is reached, it reads back the input but only lines that were longer than 10 characters.
I am on Linux, so EOF is Ctrl + D, but, if an input line is longer than 10, it prints when I push enter, rather than waiting until EOF is reached.
here is my code:
#define MAXSIZE 1000
#define SIZE 10
int checklen(char line[], int index);
int main()
{
char currentline[MAXSIZE];
int i = 0;
while ((currentline[i] = getchar()) != EOF){
if (currentline[i] == '\n'){
if (checklen(currentline, i) > SIZE){
printf("%s", currentline);
}
}
++i;
}
return 0;
}
int checklen(char line[], int index)
{
int i;
for (i=index; line[i] != '\n'; ++i){
;
}
return i;
}
EDIT: I have been trying to figure it out for quite a while now with no luck. I'm not really understanding what you guys are saying and everything but we'll get there eventually :)
I have since rewritten the code but it is still not working.
#include <stdio.h>
#define MAX 1000
#define SIZE 10
void examine(char input[], int index);
int main()
{
int i=0;
char input[MAX];
char output[MAX];
//take user input and store it in our input string
while ((input[i] = getchar()) != EOF){
++i;
}
//put a null byte at the end of input[]
input[i+1] = '\0';
//examine line by line until end of string (null byte)
for (i=0; input[i] != '\0'; ++i){
if (input[i] == '\n'){
examine(input, i);
}
}
return 0;
}
void examine(char input[], int index)
{
//decrement through input[] until \n or start [0] is reached
int i=0;
for (i=0; ((input[index] != '\n') || (index > 0)); ++i){
--index;
}
//if line is bigger than 10 chars, print it
if (i>SIZE){
for (; input[index+1] != '\n'; ++index){
putchar(input[index]);
}
}
//otherwise, return
return;
}

rewrote it. was actually really easy. here is the code:
/*this program takes keyboard input from the user until EOF
and prints out their input excluding lines less than or equal to LINESIZE*/
#include <stdio.h>
#define MAX 2000
#define LINESIZE 10
void checkprint(char line[]);
int main()
{
char input[MAX];
char line[MAX];
int i, i2;
i2 = 0;
//take input until EOF (Ctrl + D)
for (i=0; (input[i]=getchar()) != EOF; ++i){
;
}
//add null byte to end of string
input[i+1] = '\0';
//basic formatting for aesthetics
putchar('\n');
//copy a line into line[] from input[] until NULL byte reached
for (i=0; input[i] != '\0'; ++i){
line[i2] = input[i];
++i2;
//if end of line, call checkprint
if (input[i] == '\n'){
checkprint(line);
i2=0;
}
}
return 0;
}
void checkprint(char line[])
{
int i;
//find the length of the line
for (i=0; line[i] != '\n'; ++i){
;
}
//if longer than LINESIZE, print it
if (i > LINESIZE){
putchar('\n');
for (i=0; line[i] != '\n'; ++i){
putchar(line[i]);
}
}
}

#include <stdio.h>
#define MAX 1000
#define SIZE 10
void examine(char input[], int index);
int main(void){
char input[MAX];
// char output[MAX];
int i, ch;
for(i=0; i< MAX - 1 && (ch = getchar()) != EOF; ++i)
input[i] = ch;
input[i] = '\0';
for (i=0; input[i] != '\0'; ++i){
if (input[i] == '\n'){
examine(input, i);
}
}
examine(input, i);//for '\0'
return 0;
}
void examine(char input[], int index){
int i;
if(index == 0) return ;
for (i=1; index - i >= 0 && input[index-i] != '\n'; ++i)
;
--i;
if (i > SIZE){
while(i>0)
putchar(input[index - i--]);
putchar('\n');
}
return;
}
buffer's size 11 version.
#include <stdio.h>
#define SIZE 10
void print(char ch){
static char buf[SIZE+1];
static index = 0, over = 0;
int i;
if(over){
putchar(ch);
if(ch == '\n')
over = 0;
return ;
}
if(ch == '\n'){
index = 0;
} else {
buf[index++] = ch;
if(index == SIZE + 1){
for(i=0;i<index;++i){
putchar(buf[i]);
}
index = 0;
over = 1;
}
}
}
int main(void){
int ch;
while((ch = getchar()) != EOF){
print(ch);
}
return 0;
}
//simple is to use the fgets

Related

Write a program to break long input lines into two or more shorter lines of length at most n

I am currently learning C and working on a problem that breaks input lines into lengths of n. Below is my current code where n is set to 30. When it reaches the n-th index it replaces that index with ' ' and then line breaks, but it will only do it for the first n characters and I'm unsure what isn't getting rest in order to it to continue making a new line at the nth index.
int getline2(void);
int c, len, cut, counter;
char line[MAXLINE];
main() {
while ((len = getline2()) > 0) {
if (len > BREAK) {
c = 0;
counter = 0;
while (c < len) {
if (line[c] == ' ') {
counter = c;
}
if (counter == BREAK) {
line[counter] = '\n';
counter = 0;
}
counter++;
c++;
}
}
printf("%s", line);
}
return 0;
}
int getline2(void) {
int c, i;
extern char line[];
for (i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c; //i gets incremented at the end of the loop
if (c == '\n') {
line[i] = c;
++i;
}
line[i] = '\0';
return i;
}
Your code is a little too complicated:
you do not need to store the bytes read from the file into an array, just output them one at a time, keeping track of the line length
when the line would become too long, output a newline and reset the count before you output the byte.
also not that none of these global variables deserves to be global.
and the prototype for main should be either int main(), int main(void) or int main(int argc, char *argv[]) or equivalent. main()` is an obsolete syntax that should be avoided.
Here is a modified version:
#include <stdio.h>
#define BREAK 30
int main() {
int c;
int len = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
putchar(c);
len = 0;
} else {
if (len >= BREAK) {
putchar('\n');
len = 0;
}
putchar(c);
len++;
}
}
return 0;
}

Why is this C program printing line longer than MAXLINE?

The program should print all the input lines which length is longer than MINLINE 5 and shorter than MAXLINE 10. Ref. K&R book exercise 1.17
#include <stdio.h>
#define MAXLINE 10
#define MINLINE 5
int getlines(char lines[], int maxline);
int main()
{
int length;
char lines[MAXLINE];
while ((length = getlines(lines, MAXLINE)) > 0)
{
if (length > MINLINE)
printf("%s", lines);
}
return 0;
}
int getlines(char lines[], int maxline)
{
int i, c;
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
{
lines[i] = c;
}
if (c == '\n')
{
lines[i] = c;
++i;
}
lines[i] = '\0';
return i;
}
Desired outpur should be like this :-
Hello\n
Hello\n
hi\n
excuseMe\n
excuseMe\n
longLineNotToBePrinted\n
done
done
but unexpectedly the program printing lines that are far longer than MAXLINE and sometimes printing those omitting some trailing characters.
For starters this function
int getlines(char lines[], int maxline)
{
int i, c;
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
{
lines[i] = c;
}
if (c == '\n')
{
lines[i] = c;
++i;
}
lines[i] = '\0';
return i;
}
has undefined behavior because it can store the character '\0' at position maxline that is outside the array lines that has the valid range of indices [0, maxline).
As for your question about the output then if you entered a text that is greater than maxline then the function will return a string that does not contain the new line character '\n'. So the next string will be outputted in the same line.
/* Updated code. Now it is working fine.
Problems were both in main() function and in the function getlines().
#include <stdio.h>
#define MAXLINE 10
#define MINLINE 5
int getlines(char lines[], int maxline);
main()
{
int length;
char lines[MAXLINE];
while ((length = getlines(lines, MAXLINE)) > 0)
{
if (length > MINLINE)
{
/* As the input line can be longer than MAXLINE and in that case there will be no '\n' escape sequence to be stored in the lines[MAXLINE] array so we have used the if block to flow the control in such a way that when the input line is longer than MAXLINE, the output string will be printed manually with a '\n' *newline character. */
if (length > MAXLINE)
printf("%s\n", lines);
else
printf("%s", lines);
}
}
return 0;
}
int getlines(char lines[], int maxline)
{
int i, j, c;
i = 0;
for (j = 0; (c = getchar()) != EOF && c != '\n'; ++j)
{
/* In the for loop this time we didn't use the condition 'j < maxline
-1' as getchar() needs to read the whole input line no matter it's length(can be greater than MAXLINE), rather we have used the 'j < maxline -1' condition as a nested if block inside the for loop. While doing this to keep the getchar() function busy reaching the last input character no matter how long the line is we have used two variable i and j to overcome the problem in such a way that i will be used to store characters in the lines[MAXLINE] array, while j will be increased untill it reaches the end of the line. */
if (j < maxline - 1)
{
i = j;
lines[i] = c;
++i;
}
}
if (c == '\n')
{
if (j < maxline - 1)
{
lines[i] = c;
++i;
++j;
}
else
++j;
}
lines[i] = '\0';
return j;
}

Unable to output the longest string

Problem Statement: write a program that reads a set of text lines and prints the longest.
Program:
#include <stdio.h>
#define MAX 100
int getlinetext(char s[]);
int main(void)
{
char longest[MAX];
int longestlenght = 0;
char line[MAX];
int lenght;
while ((lenght = getlinetext(line)) > 0){
if(lenght > longestlenght){
longestlenght = lenght;
int i = 0;
while (line[i] != '\0'){
longest[i] = line[i];
i++;
}
longest[i] = '\0';
}
}
printf("The longest lenght is %d\n", longestlenght);
printf("%s\n", longest);
return 0;
}
int getlinetext(char line[])
{
int i=0;
int c;
while ((c = getchar()) != EOF){
line[i] == c;
if (c == '\n')
break;
i++;
}
line[i] = '\0';
return i;
}
Expected Output:
hello
world!!
The longest lenght is 7
world!!
Actual Output:
hello
world!!
The longest lenght is 7
�
Somehow, I am able to print the correct longest lenght but not the string itself. I thought I miss the null byte, but it's there and the error still persist.
As #chux pointed out, i made a silly mistake by using equal sign ("==") instead of assignment sign ("=") on line #34:
line[i] == c -> line[i] = c
So the corrected program would be
#include <stdio.h>
#define MAX 100
int getlinetext(char s[]);
int main(void)
{
char longest[MAX];
int longestlenght = 0;
char line[MAX];
int lenght;
while ((lenght = getlinetext(line)) > 0){
if(lenght > longestlenght){
longestlenght = lenght;
int i = 0;
while (line[i] != '\0'){
longest[i] = line[i];
i++;
}
longest[i] = '\0';
}
}
printf("The longest lenght is %d\n", longestlenght);
printf("%s\n", longest);
return 0;
}
int getlinetext(char line[])
{
int i=0;
int c;
while ((c = getchar()) != EOF){
line[i] = c;
if (c == '\n')
break;
i++;
}
line[i] = '\0';
return i;
}

Manually counting the number of character in a string in C

I tried to manually count the number of characters in my string including any blank spaces. I coded this:
#include <stdio.h>
#include <stdlib.h>
void Unos(char* string, int duzina)
{
int i=0;
char c;
do {
c=getchar();
string[i]=c;
i++;
} while(c != '\n' && i<duzina);
}
int brojznak(char* str)
{
int i=0,br=0;
while(*str++ != '\0')
{
br++;
}
return br;
}
int main()
{
char recenica[100];
printf("Unesite recenicu\n");
Unos(recenica,100);
int i=0;
printf("%d",brojznak(recenica));
return 0;
}
This code doesn't work correctly, but I think it should. On the other hand, if we change condition:
int brojznak(char* str)
{
int i=0,br=0;
while(*str++ != '\0')
{
br++;
}
return br;
}
It again sometimes prints some random characters, but counts it accurately. Can anyone tell me what is wrong in this code?
String termination missing in Unos().
while(*str++ != '\0') in brojznak() does not know when to stop.
void Unos(char* string, int duzina) {
int i=0;
char c;
do {
c = getchar();
string[i] = c;
i++;
} while(c != '\n' && (i + 1) <duzina); // Insure enough room
string[i]='\0'; // add this
}
Some other refinements:
void Unos2(char* string, size_t duzina) {
size_t i = 0;
while ((i+1) < duzina) { Only attempt to read if space allows it.
int c = getchar(); // Use int to distinguish EOF from characters
if (c == EOF) break;
string[i] = c;
i++;
if (c == '\n') break;
}
string[i] = '\0'; // add this
}
try this
for(i=0; getchar()!='\n'; ++i)
;

Can't figure out why I am getting strange output

I'm pretty novice. I've been working through the K&R C programming book, and one of the exercises was to write a program that prints any input lines that are longer than 80. Here's my code:
include <stdio.h>
#define MAXLINE 1000
int getaline(char line[], int maxline);
int main()
{
int i, c;
char line[MAXLINE];
if ((c = getaline(line, MAXLINE)) > 80){
for (i =0; i < MAXLINE; ++i)
if (c != '\0')
printf("%c", line[i]);
printf("\n");
}
}
/* reads a line into S, returns the length of that line */
int getaline(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
So I'll pipe a line longer than 80 chars to the compiled program. Here's the output:
cat input.txt | ./a.out
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
??i?F???4?w??>&Y?_???xf?7U?h#?
??i??7U?v??i??7U??7U?(?7U?#?
H??i?8?7U??7U?
It prints the line, but it gives all this anomalous output. I tried to figure out why, but I just can't seem to find why.
However, I am pretty sure that the problem lies in the getaline function.
Any help would be greatly appreciated! :)
You are confusing c with the current character.
Try this, change this
for (i =0; i < MAXLINE; ++i)
with
for (i = 0 ; i < c ; ++i)
and the check should be
if (line[i] != '\0')
instead of
if (c != '\0')
your getaline() function returns, i the position of the last character read, you are comparing it with the null termination byte.
Also if you are null terminating the string, why don't you just
printf("%s\n", line);
this code should work
#include <stdio.h>
#define MAXLINE 1000
int getaline(char line[], int maxline);
int main()
{
int i, c;
char line[MAXLINE];
if ((c = getaline(line, MAXLINE)) > 80)
printf("%s\n", line);
return 0; // you must return from main()
}
/* reads a line into S, returns the length of that line */
int getaline(char s[], int lim)
{
int c, i;
for (i = 0 ; (i < lim - 1) && ((c = getchar()) != EOF) && (c != '\n') ; ++i)
s[i] = c;
if (c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
You're checking c as though it were a character, when it's the line-length count; it will never be equal to \0, so you keep pringting.
if ((c = getaline(line, MAXLINE)) > 80){
// c now equals, let's say, 81
for (i =0; i < MAXLINE; ++i)
// c is still 81, we never fail
if (c != '\0')
printf("%c", line[i]);
printf("\n");
}
Consider this instead:
if (getaline(line, MAXLINE) > 80) {
for (i =0; i < MAXLINE; ++i)
{
c = line[i];
if (c != '\0')
printf("%c", c;
else
break;
}
printf("\n");
}
Replace
for (i =0; i < MAXLINE; ++i)
if (c != '\0')
printf("%c", line[i]);
by
for (i =0; i < c; ++i)
printf("%c", line[i]);

Resources