Some time off to relax and a new C function
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';
}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
Note: Your comment may require approval before it is posted to the site.