The C Programming Language (TCPL): Chapter 4

Exercise 4-13. Write a recursive version of the function reverse(s), which reverses the string s in place.

#include
#include

void switchchar(char s[], int i, int j)
{
char tmp;

tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}

void reverse(char s[])
{
static int i = 0;
static int len = 0;

if(i == 0)
{
len = strlen(s) - 1;
}

switchchar(s, i, (len - i));

i++;
if(i <= (len / 2)) { reverse(s); } } int main() { char str[] = "ABCDEF123456"; reverse(str); printf("%sn", str); return 0; }

The C Programming Language (TCPL): Chapter 3

Ex 3-3. Write a function expand(s1, s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc…xyz in s2. Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. Arrange that a leading – is taken literally.

#include
#include

void expand(char s1[], char s2[])
{

int i = 0;
char reverse = 0;

/* check if reverse */
if (s1[0] == '-')
{
reverse = 1;
i++;
}

int l = 0;
int end, start, ready;

start = 0;
end = 0;
ready = 0;
for(; s1[i] != ''; i++)
{
if(s1[i] != '-')
{
if (start == 0)
{
start = s1[i];
}
else
{
end = s1[i];
ready = 1;
}
}
else
{
/* for x-X-X */
if(ready == 0 && start == 0)
{
start = s1[i - 1];
l--;
}
}

if(ready == 1)
{
int k;
if(reverse == 0)
{
for(k = start; k <= end; k++, l++) { s2[l] = k; } } else { for(k = end; k >= start; k--, l++)
{
s2[l] = k;
}
}

start = 0; end = 0; ready = 0;
}

}

}

int main()
{
char s1[64], s2[256];

strncpy(s1, "a-b-ca-z0-9A-Z", 64);
strncpy(s2, "", 256);
expand(s1, s2);
printf("%s => %sn", s1, s2);

return 0;

}

The C Programming Language (TCPL): Chapter 1

This is definitely my most favorite C book and probably one of the best programming books ever written. If you want to dive into C, this is the perfect book. However, I won’t recommend it if you don’t know the basics.
In this series I will post my solutions for some more interesting exercises.

Exercise 1-9. Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include

int main()
{
char c;
while((c = getchar()) != EOF)
{
if (c == ' ')
{
putchar(c);
while((c = getchar()) == ' ')
{
;
}
}

putchar(c);
}
return 0;
}

Exercise 1-19. Write a function reverse(s) that reverses the character string s.

#include

int length(char s[])
{
int i;
for(i = 0; s[i] != ''; i++)
;
return i;
}

void switchchar(char s[], int i, int j)
{
char tmp;

tmp = s[i];
s[i] = s[j];
s[j] = tmp;

}

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

len = length(s) - 1;

for(i = 0; i < (len / 2); i++) { switchchar(s, i, (len - i)); } } int main() { char s[] = "abcdefghi"; reverse(s); return 0; }

Small features, big impacts

APL. Lisp. VBA. These are all programming languages. Everyone offers various modules, syntaxes and ideas. One thing is interesting. The more advanced a programming languages is that you’ve used, the more you wish their features in other languages. Lisp is certainly the most famous example with dynamic typing, garbage collection and macros.
I do really enjoy high level programming. I adapt quite easily to many features of various programming languages. Beginning with garbage collection to pattern matching and if I return after a long time from Python and Haskell to C or C++ I really miss some features.
A very simple example.

#define MAX_VAL 100
int values[MAX_VAL] = {1, 9, 2, ...};
int i;
int sum = 0;
for(i = 0; i < MAX_VAL; i++)
    sum += values[i]
printf("Sum is %i\n", sum);

vs.

values = [1, 9, 2, ...]
print "Sum is", sum(values)

The second solution avoids even side-effects.
However, I understand some things about esr’s attitude better. He wrote:

I suggest you should use Python, to avoid C programming on jobs that don’t require C’s machine efficiency.

(How to Become A Hacker)