for(p = buf; (*p = ((p == buf) || *(p-1) == ' ' ? toupper(*p) : *p)) != '\0'; p++);Isn't it nice that C enables you to put so much power in a single line of code? Quick: What does the preceding line of code do?
for(p = buf; *p != '\0'; p++) {
if(p == buf)
*p = toupper(*p);
else if(*(p-1) == ' ')
*p = toupper(*p);
}
It converts a string to title case. In the latter's for loop, the first part does
nothing but initialization, the middle part does nothing but
comparison, the last part does nothing but incrementation. The actual
data processing is done in the block associated with the for loop, and it's plain to see
that the first line of the block uppercases the first character of the
string, and the second line of the block changes to uppercase any
character preceded by a space.
#!/usr/bin/ruby
|
#!/usr/bin/ruby
|