hello this is the title

Hello, World

Here is some C practice.

#include <stdio.h>

main()
{
    printf("HELLO, world\n");
}
HELLO, world

Temperature Conversions

Here is the basic temperature conversion example from Section 1.2 of Kernighan and Ritchie.

#include <stdio.h>

/* print Fahrenheit-Celsius table
 * for fahr = 0, 20, ..., 300 */

main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;

    while (fahr <= upper) {
        celsius = 5 * (fahr-32)/9;
        printf("%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Here is the output:

0	-17
20	-6
40	4
60	15
80	26
100	37
120	48
140	60
160	71
180	82
200	93
220	104
240	115
260	126
280	137
300	148

Right-Align Degrees

Prefer right-aligning the columns.

#include <stdio.h>

/* print Fahrenheit-Celsius table
 * for fahr = 0, 20, ..., 300 */

main()
{
    int fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;

    while (fahr <= upper) {
        celsius = 5 * (fahr-32)/9;
        printf("% 4d\t%4d\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Here is the output:

   0	 -17
  20	  -6
  40	   4
  60	  15
  80	  26
 100	  37
 120	  48
 140	  60
 160	  71
 180	  82
 200	  93
 220	 104
 240	 115
 260	 126
 280	 137
 300	 148

Change to Floats

Also add a heading to table (Exercise 1-3)

#include <stdio.h>

/* print Fahrenheit-Celsius table
 * for fahr = 0, 20, ..., 300 */

main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;

    printf("%3s\t%6s\n", "F", "C");
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f\t%6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Here is the output:

  F	     C
  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

Celsius to Fahrehheit

As per Exercise 1-4.

#include <stdio.h>

/* print Fahrenheit-Celsius table
 * for celsius = -20, -10, .., 150 */

main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = -20;
    upper = 150;
    step = 10;

    celsius = lower;

    printf("%3s\t%6s\n", "C", "F");
    while (celsius <= upper) {
        fahr = (9.0/5.0) * celsius + 32.0;
        printf("%3.0f\t%6.1f\n",  celsius, fahr);
        celsius = celsius + step;
    }
}

Here is the output:

  C	     F
-20	  -4.0
-10	  14.0
  0	  32.0
 10	  50.0
 20	  68.0
 30	  86.0
 40	 104.0
 50	 122.0
 60	 140.0
 70	 158.0
 80	 176.0
 90	 194.0
100	 212.0
110	 230.0
120	 248.0
130	 266.0
140	 284.0
150	 302.0

Using For Statement

#include <stdio.h>

/* print Fahrenheit-Celsius table */

main()
{
    int fahr;

    for (fahr = 0; fahr <= 300; fahr = fahr+20)
        printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));
}

Here is the output:

  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

Reversing Order

#include <stdio.h>

/* print Fahrenheit-Celsius table in reverse order */

main()
{
    int fahr;

    for (fahr = 300; fahr >= 0; fahr = fahr-20)
        printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));
}

Here is the output:

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

Symbolic Constants

#include <stdio.h>

#define LOWER 0
#define UPPER 300
#define STEP 20

/* print Fahrenheit-Celsius table */

main()
{
    int fahr;

    for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
        printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));
}

Here is the output:

  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

Character Input and Output

#include <stdio.h>

/* copy input to output; 1st version */

main()
{
    int c;

    c = getchar();
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}

Here is the input which will be fed to this programme:

copyme!

Here is the transcript:


With assignment in while condition

#include <stdio.h>

/* copy input to output; 2nd version */

main()
{
    int c;

    while ((c = getchar()) != EOF) {
        putchar(c);
    }
}

Here is the input which will be fed to this programme:

copyme!

Here is the transcript:


Testing value of comparison

Verifying that the expression c != EOF is 0 or 1, as per Exercise 1-6.

#include <stdio.h>

main()
{
    int c, not_eof;

    not_eof = 1;

    while (not_eof) {
        c = getchar();
        not_eof = (c != EOF);
        printf("value of not_eof is %d\n", not_eof);
    }
}

Here is the input which will be fed to this programme:

copyme!

Here is the transcript:


Print value of EOF

As per Exercise 1-7

#include <stdio.h>

/* print value of EOF */
main()
{
    printf("value of EOF is %d\n", EOF);
}

Result of running this:

value of EOF is -1

Section 1.5.2 Character Counting

#include <stdio.h>

/* count characters in input; 1st version */

int main()
{
    long nc;

    nc = 0;

    while (getchar() != EOF) {
        ++nc;
    }

    printf("%ld\n", nc);
}

Here is the input which will be fed to this programme:

abcdefghi

Result of running this:


Increment operator fun!

#include <stdio.h>

int main()
{

    int nc;

    nc = 0;

    printf("the value of nc is %d\n", nc);
    printf("the value of nc is %d\n", ++nc);
    printf("the value of nc is %d\n", nc++);
    printf("the value of nc is %d\n", nc);

    printf("\n");

    printf("the value of nc is %d\n", nc);
    printf("the value of nc is %d\n", --nc);
    printf("the value of nc is %d\n", nc--);
    printf("the value of nc is %d\n", nc);

}
the value of nc is 0
the value of nc is 1
the value of nc is 1
the value of nc is 2

the value of nc is 2
the value of nc is 1
the value of nc is 1
the value of nc is 0

count chars with for loop

#include <stdio.h>

/* count characters in input; 2nd version */

int main()
{
    double nc;

    for (nc = 0; getchar() != EOF; ++nc)  {
    }

    printf("%.0f\n", nc);
}

Here is the input which will be fed to this programme:

abcdefghi

Result of running this:


Section 1.5.3 Line Counting

#include <stdio.h>

/* count lines in input */

int main()
{
    int c, n1;

    n1 = 0;

    while ((c = getchar()) != EOF) {
        if (c == '\n') {
            ++n1;
        }
    }

    printf("%d\n", n1);
}

Here is the input which will be fed to this programme:

abc
def
ghi
j

Result of running this:


Count Blanks, Tabs, Newlines

As per Exercise 1-8.

#include <stdio.h>

int main()
{
    int c, nb, nt, nl;

    nb = 0;
    nt = 0;
    nl = 0;

    while ((c = getchar()) != EOF) {
        if (c=='\n')
            ++nl;
        if (c=='\t')
            ++nt;
        if (c==' ')
            ++nb;
    }

    printf("\n\n");
    printf("tabs\t%d\n", nt);
    printf("spaces\t%d\n", nb);
    printf("lines\t%d\n", nl);
}

Here is the input which will be fed to this programme:

This is a line!
this    is some more.

Result of running this:


Coalesce Multiple Blanks

As per Exercise 1-9.

#include <stdio.h>

int main()
{

    int c, in_space;

    in_space = 0;

    while ((c = getchar()) != EOF) {
        if (c != ' ') {
            if (in_space)
                putchar(' ');
            putchar(c);
        }

        in_space = (c == ' ');
    }

}

Here is the input which will be fed to this programme:

This is a line!
this    is some more.

Result of running this:


Make Spaces Visible

As per Exercise 1-10.

Note that ‘else’, && have not been covered yet so writing this without using these.

#include <stdio.h>

int main()
{
    int c, already;

    while ((c = getchar()) != EOF) {
        already = 0;

        if (c == '\t')  {
            printf("\\t");
            already = 1;
        }

        if (c == '\b') {
            printf("\\b");
            already = 1;
        }

        if (c == '\\') {
            printf("\\\\");
            already = 1;
        }

        if (1 - already)
            putchar(c);
    }

}

Here is the input which will be fed to this programme:

this is a slash \
in here	is a tab

Result of running this:


1.5.4 Word Counting

#include <stdio.h>

#define IN 0 /* inside a word */
#define OUT 1 /* outside a word */

/* count lines, words and characters in input */
int main()
{
    int c, nl, nw, nc, state;

    state = OUT;
    nl = nw = nc = 0;

    while ((c = getchar()) != EOF) {
        ++nc;

        if (c == '\n')
            ++nl;

        if (c == ' ' || c == '\n' || c == '\t') {
            state = OUT;
        } else if (state == OUT) {
            state = IN;
            ++nw;
        }
    }

    printf("%d %d %d\n", nl, nw, nc);
}

Here is the input which will be fed to this programme:

these are words
here are more

Result of running this:


Print Input One Word Per Line

As per Exercise 1-12

#include <stdio.h>

#define IN 0 /* inside a word */
#define OUT 1 /* outside a word */

/* print input 1 word per line */
int main()
{
    int c, state;

    state = OUT;

    while ((c = getchar()) != EOF) {
        if (c == ' ' || c == '\n' || c == '\t') {
            state = OUT;
        } else if (state == OUT) {
            state = IN;
            putchar('\n');
            putchar(c);
        } else {
            putchar(c);
        }
    }
}

Here is the input which will be fed to this programme:

these are words
here are more

Result of running this:


1.6 Arrays

#include <stdio.h>

/* count digits, white space, others */

int main()
{
    int c, i, nwhite, nother;
    int ndigit[10];

    nwhite = nother = 0;

    for (i = 0; i < 10; ++i) {
        ndigit[i] = 0;
    }

    while ((c = getchar()) != EOF)
        if ( c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;

    printf("\ndigits\n");
    for (i = 0; i < 10; ++i) {
        printf("%c: %d times\n",'0'+i, ndigit[i]);
    }
    printf("white space = %d, other = %d\n", nwhite, nother);
}

Here is the input which will be fed to this programme:

these are 3 words
here are 3 or 4 more
69

Result of running this:


Histogram

As per exercise 1-13

#include <stdio.h>

/* print as histogram */

int main()
{
    int c, i, j, nwhite, nother;
    int ndigit[10];

    nwhite = nother = 0;

    for (i = 0; i < 10; ++i) {
        ndigit[i] = 0;
    }

    while ((c = getchar()) != EOF)
        if ( c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;

    printf("\n");
    for (i = 0; i < 10; ++i) {
        printf("%c: ", '0'+i);
        for (j = 0; j < ndigit[i]; ++j) {
            printf("*");
        }
        printf("\n");
    }

    /* Print white space */
    printf(" : ");
    for (j = 0; j < nwhite; ++j) {
        printf("*");
    }
    printf("\n");

    /* Print others */
    printf("o: ");
    for (j = 0; j < nother; ++j) {
        printf("*");
    }
    printf("\n");
}

Here is the input which will be fed to this programme:

these are 3 words
here are 3 or 4 more
69

Result of running this:


Vertical histogram.

#include <stdio.h>

/* print as vertical histogram */

int main()
{
    int c, i, j, nwhite, nother;
    int ndigit[10];
    int sum;

    nwhite = nother = 0;

    for (i = 0; i < 10; ++i) {
        ndigit[i] = 0;
    }

    while ((c = getchar()) != EOF)
        if ( c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if (c == ' ' || c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;

    printf("\n");

    /* print headers */
    for (i = 0; i < 10; i++) {
        printf("%c\t", '0'+i);
    }
    printf(" \to\t\n");

    sum = 1;
    j = 0;
    while (sum > 0) {
        sum = 0;

        /* print histogram for digit counters */
        for (i = 0; i < 10; i++) {
            if (ndigit[i] > j) {
                printf("*");
                ++sum;
            }
            printf("\t");
        }

        if (nwhite > j) {
            printf("*");
            ++sum;
        }
        printf("\t");

        if (nother > j) {
            printf("*");
            ++sum;
        }
        printf("\t");

        printf("\n");
        ++j;
    }
}

Here is the input which will be fed to this programme:

These are 3 words.
Here are 3 or 4 more.
69

Result of running this:


histogram of all chars

As per Exercise 1-14.

#include <stdio.h>

#define MAX 74
#define START '0'

int main()
{
    int c, i, j;
    int nchar[MAX];

    for (i = 0; i < MAX; ++i) {
        nchar[i] = 0;
/*        printf("Initializing space %d for char '%c'\n", i, START+i);   */
    }

    while ((c = getchar()) != EOF) {
        if (c >= START && c <= START + MAX)
            ++nchar[c-START];
    }

    for (i = 0; i < MAX; ++i) {
        printf("\n%c: ", START+i);
        for (j = 0; j < nchar[i]; ++j)
            printf("*");
    }

}

Here is the input which will be fed to this programme:

these are 3 words
here are 3 or 4 more
69

Result of running this:


1.7 Functions

#include <stdio.h>

int power(int m, int n);

/* test power function */

int main()
{
    int i;

    for (i = 0; i < 10; ++i) {
        printf("%6d %6d %6d\n", i, power(2, i), power(-3, i));
    }

    return 0;
}

/* power: raise base to n-th power; n >= 0 */

int power(int base, int n)
{
    int i, p;

    p = 1;
    for (i = 1; i <= n; ++i)
        p = p * base;
    return p;
}

Result of running this:

     0      1      1
     1      2     -3
     2      4      9
     3      8    -27
     4     16     81
     5     32   -243
     6     64    729
     7    128  -2187
     8    256   6561
     9    512 -19683

Exercise 1-15 rewrite temperature conversion using functions.

#include <stdio.h>

double fahr_to_celsius(double fahr)
{
    return (5.0/9.0) * (fahr - 32.0);
}

double celsius_to_fahr(double celsius)
{
    return (9.0/5.0) * celsius + 32;
}

int main()
{
    int fahr;

    for (fahr = 0; fahr < 300; fahr = fahr + 20)
        printf("%3d %6.f\n", fahr, fahr_to_celsius(fahr));
}

Result of running this:

  0    -18
 20     -7
 40      4
 60     16
 80     27
100     38
120     49
140     60
160     71
180     82
200     93
220    104
240    116
260    127
280    138

1.9 Character Arrays

#include <stdio.h>
#define MAXLINE 1000 /* maximum input line size */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line */

int main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0)
        printf("\n\nThe longest line of input, at %d characters, was\n%s", max, longest);
    return 0;
}

int getline(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] = '';
    return i;
}

void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '')
        ++i;
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


Modified as per Exercise 1-16 to still print correct length if lines are too long.

#include <stdio.h>
#define MAXLINE 80 /* maximum input line size */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line */

int main()
{
    int len; /* current line length */
    int max; /* maximum length seen so far */
    char line[MAXLINE];
    char longest[MAXLINE];

    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
            max = len;
            copy(longest, line);
        }
    if (max > 0) {
        printf("\n\nThe longest line of input, at %d characters, was\n%s", max, longest);
        if (max > MAXLINE)
            printf("...\n"); /* show this was truncated */
    }

    return 0;
}

int getline(char s[], int lim)
{
    int c, i;
    for (i = 0; (c=getchar()) != EOF && c!='\n'; ++i)
        if (i < lim - 1)
            s[i] = c;
    if (c == '\n') {
        if (i < lim - 1)
            s[i] = c;
        ++i;
    }
    s[i] = '';
    return i;
}

void copy(char to[], char from[])
{
    int i;

    i = 0;
    while ((to[i] = from[i]) != '')
        ++i;
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


Exercise 1-17 print all lines longer than 80 chars.

#include <stdio.h>
#define MAXLINE 60

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

int main()
{
    char line[MAXLINE];

    while (getline(line, MAXLINE))
        ;

    return 1;
}

int getline(char line[], int maxline)
{
    int c, i;

    for (i = 0; (c = getchar()) != EOF && c != '\n'; i++) {
        if (i < MAXLINE-1)
            line[i] = c;
        if (i == MAXLINE-1) {
            line[i] = '';
            printf("\n\nThis line has more than %d chars:\n", MAXLINE);
            printf(line);
        }
        if (i >= MAXLINE-1)
            putchar(c);
    }

    return c != EOF;
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


Trailing Spaces

Exercise 1-18 remove trailing blanks and tabs.

#include <stdio.h>
#define MAXBLANK 20

int strip();

int main()
{
    char blanks[MAXBLANK];

    while ( rstrip(blanks) )
        ;

    return 0;
}

int rstrip(char blanks[])
{
    int c, i, j, allblank;

    i = 0;
    allblank = 1;

    while ((c = getchar()) != EOF && c!='\n') {
        if (c == ' ' || c == '\t') {
            if (i < MAXBLANK-1) {
                blanks[i++] = c;
            }
        }
        else {
            allblank = 0;
            if (i > 0) {
                for (j = 0; j < i; j++)
                    putchar(blanks[j]);
                i = 0;
            }
            putchar(c);
        }
    }

    if (1-allblank)
        printf("*\n");

    return (c != EOF);
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.    xx
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


Reverse

#include <stdio.h>
#define MAXLINE 100

int readline(char line[], int max);
void reverse(char original[], char reversed[]);

int main()
{
    char original[MAXLINE];
    char reversed[MAXLINE];

    while (readline(original, MAXLINE)) {
        reverse(original, reversed);
        printf("reversed: %s\n", reversed);
    }

    return 0;
}

int readline(char line[], int max)
{
    int c, i;
    i = 0;
    while ((c = getchar()) != EOF && c != '\n' && i < MAXLINE-1) {
        line[i++] = c;
    }
    line[i] = '';

    return c != EOF;
}

void reverse(char original[], char reversed[])
{
    int i, len;

    len = 0;
    while ( original[len++] != '' )
        ;

    for (i = 0; i < len - 1; i++) {
        reversed[i] = original[len-i-2];
    }

    reversed[len-1] = '';
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


1.10 External Variables and Scope

#include <stdio.h>
#define MAXLINE 1000 /* max input line size */

int max;  /* maximum length so far */
char line[MAXLINE]; /* current input line */
char longest[MAXLINE]; /* longest line */

int getline(void);
void copy(void);

int main()
{
    int len;
    extern int max;
    extern char longest[];

    max = 0;
    while ((len = getline()) > 0)
        if (len > max) {
            max = len;
            copy();
        }

    if (max > 0) /* there was a line */
        printf("%s", longest);

    return 0;
}

int getline(void)
{
    int c, i;
    extern char line[];

    for (i=0; i < MAXLINE-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        line[i] = c;
    if (c=='\n') {
        line[i] = c;
        ++i;
    }
    line[i] = '';
    return i;
}

void copy(void)
{
    int i;
    extern char line[], longest[];

    i = 0;
    while ((longest[i] = line[i]) != '')
        ++i;
}

Here is the input which will be fed to this programme:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam gravida rhoncus turpis, sed luctus ligula pharetra sit amet.
Suspendisse porta elit vel massa volutpat id aliquam enim semper.
Praesent faucibus neque eu lectus pulvinar id vestibulum orci accumsan.
Praesent ac lectus in nibh venenatis bibendum quis non nisl.
Vivamus volutpat arcu volutpat est feugiat non lobortis justo vulputate.
Morbi vitae erat a purus elementum gravida nec nec tortor.

Nulla lacinia turpis eget enim condimentum scelerisque.
Integer viverra quam ut velit faucibus id vestibulum ligula vulputate.
Donec aliquet diam in erat adipiscing sit amet adipiscing ligula consectetur.
Suspendisse eget lacus at erat vulputate sodales.
Donec eget erat ligula, eget luctus ligula.

Result of running this:


Source of This Document

Dexy config JSON file:


HTML source:

<h1>Hello, World</h1>

Here is some C practice.

{{ d['001.c|pyg'] }}

<pre>
{{ d['001.c|c'] }}
</pre>

<h1>Temperature Conversions</h1>

Here is the basic temperature conversion example from Section 1.2 of Kernighan and Ritchie.

{{ d['002.c|pyg'] }}
Here is the output:

<pre>
{{ d['002.c|c'] }}
</pre>

<h2>Right-Align Degrees</h2>

Prefer right-aligning the columns.

{{ d['003.c|pyg'] }}

Here is the output:

<pre>
{{ d['003.c|c'] }}
</pre>

<h2>Change to Floats</h2>

Also add a heading to table (Exercise 1-3)

{{ d['004.c|pyg'] }}

Here is the output:
<pre>
{{ d['004.c|c'] }}
</pre>

<h2>Celsius to Fahrehheit</h2>

As per Exercise 1-4.

{{ d['005.c|pyg'] }}
Here is the output:
<pre>
{{ d['005.c|c'] }}
</pre>

<h2>Using For Statement</h2>

{{ d['006.c|pyg'] }}
Here is the output:
<pre>
{{ d['006.c|c'] }}
</pre>

<h2>Reversing Order</h2>

{{ d['007.c|pyg'] }}
Here is the output:
<pre>
{{ d['007.c|c'] }}
</pre>

<h1>Symbolic Constants</h1>
{{ d['008.c|pyg'] }}
Here is the output:
<pre>
{{ d['008.c|c'] }}
</pre>

<h1>Character Input and Output</h1>
{{ d['009.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['009-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['009.c|cint'] }}
</pre>

<h2>With assignment in while condition</h2>

{{ d['010.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['010-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['010.c|cint'] }}
</pre>

<h2>Testing value of comparison</h2>

Verifying that the expression c != EOF is 0 or 1, as per Exercise 1-6.

{{ d['011.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['011-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['011.c|cint'] }}
</pre>

<h2>Print value of EOF</h2>

As per Exercise 1-7

{{ d['012.c|pyg'] }}

Result of running this:

<pre>
{{ d['012.c|c'] }}
</pre>

<h2>Section 1.5.2 Character Counting</h2>

{{ d['013.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['013-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['013.c|cint'] }}
</pre>

<h4>Increment operator fun!</h4>
{{ d['014.c|pyg'] }}
<pre>
{{ d['014.c|c'] }}
</pre>

<h3>count chars with for loop</h3>
{{ d['015.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['015-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['015.c|cint'] }}
</pre>

<h2>Section 1.5.3 Line Counting</h2>

{{ d['016.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['016-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['016.c|cint'] }}
</pre>

<h3>Count Blanks, Tabs, Newlines</h3>

As per Exercise 1-8.

{{ d['017.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['017-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['017.c|cint'] }}
</pre>

<h3>Coalesce Multiple Blanks</h3>

As per Exercise 1-9.

{{ d['018.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['018-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['018.c|cint'] }}
</pre>

<h3>Make Spaces Visible</h3>

As per Exercise 1-10.

Note that 'else', &amp;&amp; have not been covered yet so writing this without using these.

{{ d['019.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['019-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['019.c|cint'] }}
</pre>

<h2>1.5.4 Word Counting</h2>

{{ d['020.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['020-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['020.c|cint'] }}
</pre>

<h3>Print Input One Word Per Line</h3>
As per Exercise 1-12

{{ d['021.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['021-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['021.c|cint'] }}
</pre>

<h2>1.6 Arrays</h2>

{{ d['022.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['022-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['022.c|cint'] }}
</pre>

<h4>Histogram</h4>

As per exercise 1-13

{{ d['023.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['023-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['023.c|cint'] }}
</pre>

Vertical histogram.

{{ d['024.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['024-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['024.c|cint'] }}
</pre>

<h3>histogram of all chars</h3>

As per Exercise 1-14.

{{ d['025.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['025-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['025.c|cint'] }}
</pre>

<h1>1.7 Functions</h1>

{{ d['026.c|pyg'] }}

Result of running this:
<pre>
{{ d['026.c|c'] }}
</pre>

Exercise 1-15 rewrite temperature conversion using functions.

{{ d['027.c|pyg'] }}

Result of running this:
<pre>
{{ d['027.c|c'] }}
</pre>

<h1>1.9 Character Arrays</h1>

{{ d['028.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['028-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['028.c|cint'] }}
</pre>

Modified as per Exercise 1-16 to still print correct length if lines are too long.

{{ d['029.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['029-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['029.c|cint'] }}
</pre>

Exercise 1-17 print all lines longer than 80 chars.

{{ d['030.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['030-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['030.c|cint'] }}
</pre>

<h4>Trailing Spaces</h4>
Exercise 1-18 remove trailing blanks and tabs.

{{ d['031.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['031-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['031.c|cint'] }}
</pre>

<h4>Reverse</h4>

{{ d['032.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['032-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['032.c|cint'] }}
</pre>

<h1>1.10 External Variables and Scope</h1>

{{ d['033.c|pyg'] }}

<div id="bookmark"></div>
Here is the input which will be fed to this programme:
<pre>
{{ d['033-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['033.c|cint'] }}
</pre>

<h1 id="source">Source of This Document</h1>

Dexy config JSON file:
<pre>
{{ d['journal.json|dexy'] }}
</pre>

HTML source:
<pre>
{{ d['journal.html|pyg'] }}
</pre>
Posted in Uncategorized | Leave a comment

hello this is the title

Posted in Uncategorized | Leave a comment

hello this is the title

Posted in Uncategorized | Leave a comment

hello this is the title

Hello, World

Here is some C practice.


Temperature Conversions

Here is the basic temperature conversion example from Section 1.2 of Kernighan and Ritchie.

Here is the output:


Right-Align Degrees

Prefer right-aligning the columns.

Here is the output:


Change to Floats

Also add a heading to table (Exercise 1-3)

Here is the output:


Celsius to Fahrehheit

As per Exercise 1-4.

Here is the output:


Using For Statement

Here is the output:


Reversing Order

Here is the output:


Symbolic Constants

Here is the output:


Character Input and Output

Here is the input which will be fed to this programme:


Here is the transcript:


With assignment in while condition

Here is the input which will be fed to this programme:


Here is the transcript:


Testing value of comparison

Verifying that the expression c != EOF is 0 or 1, as per Exercise 1-6.

Here is the input which will be fed to this programme:


Here is the transcript:


Print value of EOF

As per Exercise 1-7

Result of running this:


Section 1.5.2 Character Counting

Here is the input which will be fed to this programme:


Result of running this:


Increment operator fun!


count chars with for loop

Here is the input which will be fed to this programme:


Result of running this:


Section 1.5.3 Line Counting

Here is the input which will be fed to this programme:


Result of running this:


Count Blanks, Tabs, Newlines

As per Exercise 1-8.

Here is the input which will be fed to this programme:


Result of running this:


Coalesce Multiple Blanks

As per Exercise 1-9.

Here is the input which will be fed to this programme:


Result of running this:


Make Spaces Visible

As per Exercise 1-10.

Note that ‘else’, && have not been covered yet so writing this without using these.

Here is the input which will be fed to this programme:


Result of running this:


1.5.4 Word Counting

Here is the input which will be fed to this programme:


Result of running this:


Print Input One Word Per Line

As per Exercise 1-12

Here is the input which will be fed to this programme:


Result of running this:


1.6 Arrays

Here is the input which will be fed to this programme:


Result of running this:


Histogram

As per exercise 1-13

Here is the input which will be fed to this programme:


Result of running this:


Vertical histogram.

Here is the input which will be fed to this programme:


Result of running this:


histogram of all chars

As per Exercise 1-14.

Here is the input which will be fed to this programme:


Result of running this:


1.7 Functions

Result of running this:


Exercise 1-15 rewrite temperature conversion using functions.

Result of running this:


1.9 Character Arrays

Here is the input which will be fed to this programme:


Result of running this:


Modified as per Exercise 1-16 to still print correct length if lines are too long.

Here is the input which will be fed to this programme:


Result of running this:


Exercise 1-17 print all lines longer than 80 chars.

Here is the input which will be fed to this programme:


Result of running this:


Trailing Spaces

Exercise 1-18 remove trailing blanks and tabs.

Here is the input which will be fed to this programme:


Result of running this:


Reverse

Here is the input which will be fed to this programme:


Result of running this:


1.10 External Variables and Scope

Here is the input which will be fed to this programme:


Result of running this:


Source of This Document

Dexy config JSON file:


HTML source:

<h1>Hello, World</h1>

Here is some C practice.

{{ d['001.c|pyg'] }}

<pre>
{{ d['001.c|c'] }}
</pre>

<h1>Temperature Conversions</h1>

Here is the basic temperature conversion example from Section 1.2 of Kernighan and Ritchie.

{{ d['002.c|pyg'] }}
Here is the output:

<pre>
{{ d['002.c|c'] }}
</pre>

<h2>Right-Align Degrees</h2>

Prefer right-aligning the columns.

{{ d['003.c|pyg'] }}

Here is the output:

<pre>
{{ d['003.c|c'] }}
</pre>

<h2>Change to Floats</h2>

Also add a heading to table (Exercise 1-3)

{{ d['004.c|pyg'] }}

Here is the output:
<pre>
{{ d['004.c|c'] }}
</pre>

<h2>Celsius to Fahrehheit</h2>

As per Exercise 1-4.

{{ d['005.c|pyg'] }}
Here is the output:
<pre>
{{ d['005.c|c'] }}
</pre>

<h2>Using For Statement</h2>

{{ d['006.c|pyg'] }}
Here is the output:
<pre>
{{ d['006.c|c'] }}
</pre>

<h2>Reversing Order</h2>

{{ d['007.c|pyg'] }}
Here is the output:
<pre>
{{ d['007.c|c'] }}
</pre>

<h1>Symbolic Constants</h1>
{{ d['008.c|pyg'] }}
Here is the output:
<pre>
{{ d['008.c|c'] }}
</pre>

<h1>Character Input and Output</h1>
{{ d['009.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['009-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['009.c|cint'] }}
</pre>

<h2>With assignment in while condition</h2>

{{ d['010.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['010-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['010.c|cint'] }}
</pre>

<h2>Testing value of comparison</h2>

Verifying that the expression c != EOF is 0 or 1, as per Exercise 1-6.

{{ d['011.c|pyg'] }}

Here is the input which will be fed to this programme:

<pre>
{{ d['011-input.txt|dexy'] }}
</pre>

Here is the transcript:

<pre>
{{ d['011.c|cint'] }}
</pre>

<h2>Print value of EOF</h2>

As per Exercise 1-7

{{ d['012.c|pyg'] }}

Result of running this:

<pre>
{{ d['012.c|c'] }}
</pre>

<h2>Section 1.5.2 Character Counting</h2>

{{ d['013.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['013-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['013.c|cint'] }}
</pre>

<h4>Increment operator fun!</h4>
{{ d['014.c|pyg'] }}
<pre>
{{ d['014.c|c'] }}
</pre>

<h3>count chars with for loop</h3>
{{ d['015.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['015-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['015.c|cint'] }}
</pre>

<h2>Section 1.5.3 Line Counting</h2>

{{ d['016.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['016-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['016.c|cint'] }}
</pre>

<h3>Count Blanks, Tabs, Newlines</h3>

As per Exercise 1-8.

{{ d['017.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['017-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['017.c|cint'] }}
</pre>

<h3>Coalesce Multiple Blanks</h3>

As per Exercise 1-9.

{{ d['018.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['018-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['018.c|cint'] }}
</pre>

<h3>Make Spaces Visible</h3>

As per Exercise 1-10.

Note that 'else', &amp;&amp; have not been covered yet so writing this without using these.

{{ d['019.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['019-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['019.c|cint'] }}
</pre>

<h2>1.5.4 Word Counting</h2>

{{ d['020.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['020-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['020.c|cint'] }}
</pre>

<h3>Print Input One Word Per Line</h3>
As per Exercise 1-12

{{ d['021.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['021-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['021.c|cint'] }}
</pre>

<h2>1.6 Arrays</h2>

{{ d['022.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['022-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['022.c|cint'] }}
</pre>

<h4>Histogram</h4>

As per exercise 1-13

{{ d['023.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['023-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['023.c|cint'] }}
</pre>

Vertical histogram.

{{ d['024.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['024-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['024.c|cint'] }}
</pre>

<h3>histogram of all chars</h3>

As per Exercise 1-14.

{{ d['025.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['025-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['025.c|cint'] }}
</pre>

<h1>1.7 Functions</h1>

{{ d['026.c|pyg'] }}

Result of running this:
<pre>
{{ d['026.c|c'] }}
</pre>

Exercise 1-15 rewrite temperature conversion using functions.

{{ d['027.c|pyg'] }}

Result of running this:
<pre>
{{ d['027.c|c'] }}
</pre>

<h1>1.9 Character Arrays</h1>

{{ d['028.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['028-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['028.c|cint'] }}
</pre>

Modified as per Exercise 1-16 to still print correct length if lines are too long.

{{ d['029.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['029-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['029.c|cint'] }}
</pre>

Exercise 1-17 print all lines longer than 80 chars.

{{ d['030.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['030-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['030.c|cint'] }}
</pre>

<h4>Trailing Spaces</h4>
Exercise 1-18 remove trailing blanks and tabs.

{{ d['031.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['031-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['031.c|cint'] }}
</pre>

<h4>Reverse</h4>

{{ d['032.c|pyg'] }}

Here is the input which will be fed to this programme:
<pre>
{{ d['032-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['032.c|cint'] }}
</pre>

<h1>1.10 External Variables and Scope</h1>

{{ d['033.c|pyg'] }}

<div id="bookmark"></div>
Here is the input which will be fed to this programme:
<pre>
{{ d['033-input.txt|dexy'] }}
</pre>

Result of running this:
<pre>
{{ d['033.c|cint'] }}
</pre>

<h1 id="source">Source of This Document</h1>

Dexy config JSON file:
<pre>
{{ d['journal.json|dexy'] }}
</pre>

HTML source:
<pre>
{{ d['journal.html|pyg'] }}
</pre>
Posted in Uncategorized | Leave a comment

hello this is the title

lorem ipsum dolor et

Posted in Uncategorized | Leave a comment

Hello world!

OpenScaffold!

Posted in Uncategorized | Leave a comment