Whether it’s Apple versus Android, Linux versus anything else or, TechnologyA versus TechnologyB, tech fanboys are everywhere. I’m ashamed to admit that I used to be one, but proud to say that I no longer am.
K&R – Solution to Exercise 1.23
I’m working my way through The C Programming Language at the moment, and so far I’m loving C’s purity and leanness.
One of my few disappointments with the book is the lack of solutions to the exercises. The exercises are refreshingly challenging because the book isn’t aimed at beginning programmers. Unfortunately this means that the desire to validate one’s solutions is quite strong. Fortunately, Google had a solution to this problem: Richard Heathfield’s solutions site.
I just finished my solution to exercise 1.23 (“Remove all comments from a C program”) and I’m quite chuffed with it so thought I would share it here. Out of the multiple solutions provided on Richard’s site, mine was about the closest to following the guidelines as far as what knowledge one is supposed to have of C by that (early) point in the book (the only thing I cheated with is the use of break
and continue
– it would have just been quite ugly without those two small additions). Basically the solution is a state machine using if / else
instead of more traditional switch
methods as switch
had not yet been introduced.
My solution deals with all the special cases I could think of, and even deals with the tricky sample input on Richard’s site, provided at the bottom of the solutions page for this exercise.
/*
* K&R Exercise 1-23
*
* "Write a program to remove all comments from a C program. Don't
* forget to handle quoted strings and character constants properly.
* C comments don't nest."
*
*/
#include <stdio.h>
int main()
{
int c;
int c2;
int in_quotes = 0;
int in_comment = 0;
int current_quote;
c = getchar();
while (c != EOF)
{
if (!in_comment && !in_quotes)
{
if (c == '\'' || c == '"')
{
in_quotes = 1;
current_quote = c;
putchar(c);
}
else if (c == '/')
{
c2 = getchar();
if (c2 != EOF && c2 == '*')
{
in_comment = 1;
}
else
{
putchar(c); /* Just a regular '/', so output it. */
c = c2;
continue;
}
}
else
{
putchar(c);
}
}
else if (in_comment)
{
/* Check for a closing comment. */
if (c == '*')
{
c2 = getchar();
if (c2 != EOF && c2 == '/')
{
in_comment = 0;
}
else
{
/* Don't advance to next character in stream. */
c = c2;
continue;
}
}
}
else if (in_quotes)
{
/* Skip over escaped chars. */
if (c == '\\')
{
putchar(c);
c = getchar();
}
else
{
/* Check for closing quote. */
if (c == current_quote)
in_quotes = 0;
}
putchar(c);
}
c = getchar();
}
return 0;
}
Users Are Not Stupid
Really! And, quite frankly, I’m sick and tired of programmers talking about them like this (not all programmers, some are worse than others, and all the usual disclaimery stuff applies).
Tubecaster 3 UI Mockup
Here’s a mockup of the main window for the upcoming Tubecaster 3. The release will include support for multiple downloads at once, playlist download support and built-in media conversion tools. Stay tuned!
Pre-populate Django ModelForm with Specific Queryset
I just had a situation where I was trying to filter the queryset for a ModelMultipleChoiceField based on the currently logged-on user. I was going crazy trawling through the Django docs and eventually Google. It seemed like something which should be so simple, but there was no obvious way to do it. Eventually I found the answer, and it IS simple!
Read More →