:: Re: [DNG] Studying C as told. (For …
Top Pagina
Delete this message
Reply to this message
Auteur: Edward Bartolo
Datum:  
Aan: dng
Onderwerp: Re: [DNG] Studying C as told. (For help)
Hi,

These are my worked examples. I am posting just in case some may want
to comment.

exercise1-17:
-------------------------
#include <stdio.h>

#define MAXLINE 1000 /* maximum length of line */

int lgetline(char line[], int maxline);

/* print lines longer than 80 characters */

int main() {
int len; /* current line length */
char line[MAXLINE]; /* current input line */

  int j;
  while ((len  = lgetline(line, MAXLINE)) > 0) {
    if (len >= 80) {
      /* print if len is at least 80 characters */
      for (j = 0; j < len && putchar(line[j]); ++j);
      printf("\n");
    }
  }



return 0;
}

/* getine: read a line into s, return length */
int lgetline(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;
}
--------------------------



Exercise1-18
-------------------------
#include <stdio.h>
#include <string.h>

#define MAXLINE 1000 /* maximum length of line */

int lgetline(char line[], int maxline);

/* delete trailing spaces/tabs and delete empty lines */

int main() {
int len; /* current line length */
char line[MAXLINE]; /* current input line */

  int j;
  /* print if len > 0 */
  while ((len  = lgetline(line, MAXLINE)) > 0) {
    int k = remove_trailing(line, len);
    len = k;
    for (j = 0; j <= len && putchar(line[j]); ++j);
    printf("\nline length = %d", strlen(line));
    printf("\n");
  }


return 0;
}

/* getine: read a line into s, return length */
int lgetline(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;
}


int remove_trailing(char s[], int len)
{
int i = len - 1;
while (--i && (s[i] == '\t' || s[i] == ' ' || s[i] == '\n'));
s[i + 1] = '\n';

return i + 1;
}
------------------------

Exercise1-19:
------------------------
#include <stdio.h>

#define MAXLINE 1000 /* maximum length of line */

int lgetline(char line[], int maxline);
void reverse(char s[], int len);

/* reverse lines */

int main() {
int len; /* current line length */
char line[MAXLINE]; /* current input line */

  int j;
  /* print if len > 0 */
  while ((len  = lgetline(line, MAXLINE)) > 0) {
      reverse(line, len);
      for (j = 0; j < len && putchar(line[j]); ++j);
      printf("\n");
  }



return 0;
}

/* getine: read a line into s, return length */
int lgetline(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[], int len)
{
/* Integer division is a truncating operation that discards
* the fractional part. This is what we want.
* Reason: if len is odd, the central character will always be
* in place. If not swapping half the string will do the trick.
*/

  len = len - 1;
  int i, j = len/2;
  char ch;
  for (i = 0; i < j; ++i) {
    ch = s[i];
    s[i] = s[len - i - 1];
    s[len - i - 1] = ch;
  }
}
-------------------------------------