Jump to content

Need help about arrays in C programming

oc0110

The problem I am trying to solve is to create a program which prints all lines that has a length less than 32. If EOF is inputted, the program will end.

 

Here is the program I wrote so far.

 

The problem I am having is that when I tried to solve it in a different way, the program only printed the line which was last inputted. And now this code, is a bit weird. :P

I am trying to allow all the inputs to be made such as

 

i.e.:

asdfjlaksj

asldfjalsdjkf

asdfjklasjdfkalsdjfalsjdfjaksdfljasldfjajsdfasdfadfasdfasdfasfa

asjdfkl

 

and that input should print:

asdfjlaksj

asldfjalsdjkf

asjdfkl

 

because the 3rd line is > 32.

I would be happy if you guys help me out. I have been struggling on this problem for like 2 hours. Lol

#include <stdio.h>#define MAXLINE 1000int getline(char line[], int maxline);void copy(char to[], char from[]);main(){	int len;	char line[MAXLINE];	char length[MAXLINE];	while ((len = getline(line, MAXLINE)) > 0)	if (len < 32){		copy(length, line);	}	printf("%s\n", length);	return 0;}int getline(char s[], int lim){	int c, i;	for (i = 0; i < lim - 1 && (c = getchar()) != EOF; ++i)		s[i] = c;	if (c == 'EOF') {		s[i] = c;		++i;	}	s[i] = '\0';	return i;}void copy(char to[], char from[]){	int i;	i = 0;	while ((to[i] = from[i]) != '\0')		++i;}
Link to comment
Share on other sites

Link to post
Share on other sites

How are you getting your input? Are they manually typing it or is it through an input file?

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

The input will be made by manually typing them

Link to comment
Share on other sites

Link to post
Share on other sites

the program only writes the last string because everytime you find a string shorter than 32, you write it to 'length' overwriting what was already there

Link to comment
Share on other sites

Link to post
Share on other sites

the program only writes the last string because everytime you find a string shorter than 32, you write it to 'length' overwriting what was already there

hmm.. then what method is possible to receive all the input, then store all the lines that are shorter than 32? Do I have to change the getline function? I'm a bit confused

Link to comment
Share on other sites

Link to post
Share on other sites

hmm.. then what method is possible to receive all the input, then store all the lines that are shorter than 32? Do I have to change the getline function? I'm a bit confused

you already receive all the input, the problem is that you don't store it.

you could have an array of strings (that is, a matrix of characters), so that you can keep adding strings to the list.

or you could just have a single very long "result" string, and every time you read a line shorter than 32 chars you append it to the end of this result string

you will need to understand how strings and arrays work if you don't already

Link to comment
Share on other sites

Link to post
Share on other sites

you already receive all the input, the problem is that you don't store it.

you could have an array of strings (that is, a matrix of characters), so that you can keep adding strings to the list.

or you could just have a single very long "result" string, and every time you read a line shorter than 32 chars you append it to the end of this result string

you will need to understand how strings and arrays work if you don't already

ahhh I see thanks a lot. I mean it :P

I'll try to figure out how string and arrays work more, cause it seems like i'm not understanding it much hehehe

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×