| « Power Supply | Circuitous Support » |
C is a double edged sword. On one hand, it's simple and powerful enough that, given enough effort, you can accomplish just about anything you want. However, this power is limited insomuch that you don't have many of the friendly helper-functions that exist in higher level languages, such as string manipulation for example, unless you go ahead and create them yourself.
Case in point - consider the below function used to trim trailing spaces. Submitted by Victor, this code apparently runs in a system that processes financial transactions in real time. For the sake of the system and its users, I hope that there isn't a lot of whitespace.
char *trim_right(char *str)
{
int len = strlen(str) - 1;
if(len == 0 || str[len] != ' ') return str;
str[strlen(str)-1] = '\0';
return trim_right(str);
}
|
Someone just learned about recursion, and it became the tool of choice for the next several weeks.
When you have a hammer in your hand, everything becomes a nail. |
|
Presumably...
unsigned int strlen(char* str) { if (str[0] == '\0') { return 0; } return 1 + strlen(str + 1); } |
Re: Recursive Whitespace Removal
2012-12-19 08:22
•
by
Thomas Kyte
(unregistered)
|
buffer underwrite.... it doesn't work as intended. |
| « Power Supply | Circuitous Support » |