Weekly Links - The Glorious Future Of Technology Edition


Weekly Links - No Discernible Theme Edition


Weekly Links - Our Glorious Future Edition


K&R Solutions - 1.19

Q: Write a function 'reverse(s)' that reverses the character string 's'. Use it to write a program that reverses the input one line at a time.

A:

#include <stdio.h>

// Using the same basic format is previous exercises and examples
#define MAXLINE 1000

int get_a_line(char s[], int lim);
void reverse(char s[]);

int main(void)
{
  char line[MAXLINE];

  while ((get_a_line(line, MAXLINE)) > 0) {
    reverse(line);
    printf("%s", line);
  }

  return 0;
}

int get_a_line(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;
}

void reverse(char s[])
{
  char temp[MAXLINE];
  int i, j;

  i = 0;
  while ((temp[i] = s[i]) != '\0')
    ++i;

  /* Reversing the newline looks ugly. In real life, we might strip that 
   * char of whitespace before reversing it, as per the last lesson. */
  j = 0;
  --i;
  s[i--] = '\n';
  while (i >= 0) {
    s[i] = temp[j];
    ++j;
    --i;
  }
}
./reverse 
hi
ih
sananab is a good blog
golb doog a si bananas
tacocat
tacocat


K&R Solutions - 1.18

Q: Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

A:

#include <stdio.h>
#define MAXLINE 1000

int get_a_line(char s[], int lim);
void chop(char s[], int lim);

int main(void)
{
  int len;
  char line[MAXLINE];

  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (line[0] == '\n') {
      // Nothing happens. We want to totally ignore strings that are just
      // newlines.
    } else {
      chop(line, MAXLINE);
      printf("%s<---End Of String\n", line);
    }
  }

  return 0;
}

// Same as the previous exercise.
int get_a_line(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;
}

void chop(char s[], int lim) {
  int i;

  // First we find the end of the string.
  // Notice that the the resulting value of 'i' is the length of the string.
  // Also notice that in this context, we could have just moved to the newline
  // before the null char.
  for (i = 0; s[i] != '\0'; ++i) {
  }

  // Now move backwards again, untill we find something that's not whitespace.
  for (i -= 1; s[i] == '\t' || s[i] == '\n' || s[i] == ' '; --i) {
  }

  // And we replace whatever is after with a null char.
  // We're replacing the newline, even though the exercise doesn't tell us
  // to get rid of it. This is becuse it makes the output easier to test.
  s[i+1] = '\0';
}
./trimmer
hi 	  (enter)
hi<---End Of String
how are         you?		(enter)	 
how are 	you?<---End Of String
(enter)
three little birds(enter)
three little birds<---End Of String


K&R Solutions - 1.17

Q: Write a program to print all input lines that are longer than 80 characters.

A:

/* This is really a simpler version of the longest-line program: that already
 * has most of what we need. */
#include <stdio.h>
#define MAXLINE 1000

int get_a_line(char s[], int lim);


int main(void)
{
  int len;
  char line[MAXLINE];

  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (len > 80) {
      printf("%s", line);
    }
  }

  return 0;
}

int get_a_line(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;
}
This is a short sentence, which will not be repeated.
This, however, is a very, very, very long sentence, which will be repeated back by the program.     
This, however, is a very, very, very long sentence, which will be repeated back by the program.


K&R Solutions - 1.16

Q: Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines and as much as possible of the text.

A:

...
int main(void)
{
  int len;
  int max;
  char line[MAXLINE];
  char longest[MAXLINE];

  max = 0;
  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (len > max) {
      max = len;
      copy(longest, line);
    }
    // Following tradition, we will be ignoring the null character that marks
    // the end of every string. The standard library function strlen() also
    // ignores null characters.
    printf("Length: %d\n", len - 1);
    printf("Line: %s\n", line);
  }
  if (max > 0) {
    printf("%s", longest);
  }

  return 0;
}
...
hi
Length: 2
Line: hi

there this is an 
Length: 16
Line: there this is an

arbitrarily long string
Length: 23
Line: arbitrarily long string

moo 
Length: 3
Line: moo

arbitrarily long string


K&R Solutions - 1.15

Q: Rewrite the temperature conversion program of Section 1.2 to use a function for conversion.

A:

// Compared with the original, this is much easier to read.

#include <stdio.h>
  
float fahrenheit_to_celsius (float fahr) {
  return (5.0/9.0) * (fahr-32.0);
}

int main(void) {
  int lower = 0;
  int upper = 300;
  int step = 20;
  float fahr = lower;

  while (fahr <= upper) {
    printf("%3.0f %6.1f\n", fahr, fahrenheit_to_celsius(fahr));
    fahr = fahr + step;
  }

  return 0;
}
  0  -17.8
 20   -6.7
 40    4.4
 60   15.6
 80   26.7
100   37.8
120   48.9
140   60.0
160   71.1
180   82.2
200   93.3
220  104.4
240  115.6
260  126.7
280  137.8
300  148.9


19-02-10 Weekly Links