Web Development for 2012

Alexander Dickson - Web Developer covering PHP, jQuery / Javascript, XHTML, CSS, more

Some time off to relax and a new C function

Published on Tuesday, 31st August 2010. Coochin Creek

Last Thursday, I got frustrated a lot with web development in general and decided to take a week off to enjoy some time away.

I decided to go camping with Courtnee at Coochin Creek. I spent 4 days there, and had a nice time enjoying the change of pace. No computers and no hustle and bustle, which was enjoyable (I did bring K&R, but I found it annoying to read without the possibility of trying the exercises).

I have a new C function I've been working on tonight. It is Exercise 3-3, to create a function that works very similar to PHP's range(). I tested it with the examples in K&R, and they passed :). As usual, I'm going to show you guys my approach.

void expand(const char str1[], char str2[]) { int i, j; int start = -1; int inRange = 0;   for (i = 0, j = 0 ; str1[i] != '\0'; i++) { if (str1[i] == '-') { // Leading and trailing dashes are taken literally if (i == 0 || i == strlen(str1) - 1) { str2[j++] = str1[i]; } else { inRange = 1; } } else if (inRange) { while(start < str1[i] + 1) { int validChar = (isdigit(start) || (start >= 'a' && start <= 'z') || (start >= 'A' && start <= 'Z'));   if ( ! validChar) continue; str2[j++] = start++; } inRange = 0; } else { start = str1[i]; str2[j++] = start++; } } str2[j] = '\0'; }
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 or trailing - is taken literally.

As you can see, I've finally started using some of the functions available such as strlen() and isdigit(). To use these, I've had to include more than just the standard input output library.

Comments

No comments yet.

Leave a Comment

Comment Details

Your email will never be displayed. If you have a gravatar, it will be displayed.

Note: Your comment may require approval before it is posted to the site.

Stack Overflow Profile

view full profile »

About

I'm a web developer from the Sunshine Coast, Australia. more »