~dvko/the-c-programming-language-book-exercises

1693df406d287cf1fd1819f20dea72e26aec924a — Danny van Kooten 4 years ago f0bb14c
add some exercises from chapter 1
A 1-16-longest-line/longest_line.c => 1-16-longest-line/longest_line.c +43 -0
@@ 0,0 1,43 @@
#include <stdlib.h>
#include <stdio.h>

void copy(char * dst, char * src);

#define MAXLINE 10

int main() {
	int c;
	int max = 0;
	char longest[MAXLINE];
	char line[MAXLINE];
	int llen = 0;

	while ((c = getchar()) != EOF) {
		if (c == '\n' || c == EOF) {

			if (llen > max) {
				max = llen;
				copy(longest, line);
			}

			llen = 0;
			continue;
		} 

		// only copy up to MAXLINE chars
		if (llen < MAXLINE-1) {
			line[llen] = c;
		}
		llen++;
	}

	printf("Longest line is %d characters.\n", max);
	printf("Longest line: %s\n", longest);
}

void copy(char * dst, char * src) {
	int i = 0;
	while ((dst[i] = src[i]) != '\0') {
		i++;
	}
}

A 1-17-lines-longer-than/lines_longer_than.c => 1-17-lines-longer-than/lines_longer_than.c +45 -0
@@ 0,0 1,45 @@
#include <stdio.h>
#include <stdlib.h>

#define MAXLINE 1000
#define TRESHOLD 20

void copy(char * dst, char * src);
int get_line(char line[], int max_length);

int main() {
	int c;
	char line[MAXLINE];
	int line_length = 0;
	size_t maxline = 30;
	char * line2 = malloc(30);
	while ((line_length = getline(&line2, &maxline, stdin)) > 0) {
		if (line_length > TRESHOLD) {
			printf("%d %s", line_length, line2);
		}
	}
	
}

void copy(char * dst, char * src) {
	int i = 0;
	while ((dst[i] = src[i]) != '\0') {
		i++;
	}
}

int get_line(char line[], int max_length) {
	int len = 0;
	int c;
	for (; (c = getchar()) != EOF && c != '\n' && len < max_length; ++len) {
		line[len] = c;	
	}

	if (c == '\n') {
		line[len] = '\n';
		len++;
	}
	line[len] = '\0';
	return len;
}
// last line which should also show

A 1-4-fahrenheit/fahrenheit.c => 1-4-fahrenheit/fahrenheit.c +22 -0
@@ 0,0 1,22 @@
#include <stdio.h>
/* print Fahrenheit-Celsius table
   for fahr = 0, 20, ..., 300 */
int fahr, celsius;

int main()
{
	extern int fahr;
	int lower, upper, step;
	lower = 0;
	upper = 300;
	step = 20;
	/* lower limit of temperature scale */
	/* upper limit */
	/* step size */
	fahr = lower;
	while (fahr <= upper) {
		celsius = 5 * (fahr-32) / 9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	} 
}
\ No newline at end of file