| Message |
OK. In C, a string is contained in a block of memory, where a block of memory is just a sequence of bytes. (Everything, in a sense, is contained in a block of memory; an integer is a four-byte block of memory.)
A 'string' (in C, mind you) is defined as the sequence of characters starting at a given point in memory, and continuing up until the first character equal to 0 -- that's numerical 0, not the character '0' (whose numerical value is actually 48).
So, to refer to a string, you carry around a pointer to the block of memory containing the first character of the string.
What strlen does is quite simple. It basically loops over the string, increasing the memory pointer by one character at a time, counting how many characters it finds until it finds a 0. Note that this is basically what Capitalize is doing:
char *capitalize( const char *str )
{
static char strcap[MAX_STRING_LENGTH];
int i;
for( i = 0; str[i] != '\0'; i++ )
strcap[i] = str[i];
strcap[i] = '\0';
strcap[0] = UPPER( strcap[0] );
return strcap;
}
Look at what this function is doing. It is looping over all characters in 'str', copying them all into the 'strcap' block of memory, until it finds a character in 'str' at address 'i' equal to numerical zero.
If you know that the string is of length l, then the last character is at index l-1. Therefore, using strlen will tell you how long the string is, and you can use that information to get the last character.
Then, you simply need to examine the last character to see if it's a punctuation mark or not.
If it's not punctuation, you need to set index l to a period, and set index l+1 to numerical zero, in order to mark that the string ends one character later. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | top |
|