Making something like capitalize

Posted by Metsuro on Fri 26 May 2006 12:31 AM — 16 posts, 59,931 views.

USA #0
ok well looking at capitalize... I changed it to...

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;
}

donno if it was smart to remove LOWER( str[i] ) but I did so my strings could have a Capital in the begining and more somewhere else in the string... Now what I want to do, is make something like this to check for periods and the like at the end of the string, and if there isn't a ?, !, or a period, to add a period at the end of the string, but I dont know how to do something like that.
USA #1
Just find the last character -- the one before the \0 -- and check if it's a punctuation mark. If it is, do nothing, if it isn't, change the \0 to a period and add a \0 afterwards.
USA #2
saddly, I have no idea what you said heh.
USA #3
Get the length of the string, then use an ifcheck to check the last character in the string for whatever you need.
USA #4
I'd like to note, i'm not really good with coding, i just get lucky sometimes, mind giving me an example how I might do that please?
USA #5
I keep thinking to say "string.length()" but that's C++. I think the function is called len to get the length? Check the manual for it.
USA #6
the function is called "strlen()" :)

USA #7
Now to find out how to use it...
USA #8
You can type "man strlen" to get the documentation on how the function works.

I have the impression that you're not quite clear on how strings work in C, but before I start explaining it (as it can get lengthy and I'm lazy and don't want to type unless I have to :P) could you confirm that that's the problem?

In short:
A string is a block of memory that contains a sequence of characters, ending at the first character that is equal to '\0' (== 0).
USA #9
Yea, I dont know much about coding in general, but I know how to change things to make them work the way I want, but i dont know what I am actually changing things to, and why =P
USA #10
ok so strlen, gives the lenth of a string, but I still dont know how to use that in anyway <.<
USA #11
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.
USA #12
Thanks to you explaining it a little I came up with this...

char *capper( const char *str );
{
int i;
static char temp[MAX_STRING_LENGTH];

i = strlen( str );
if( str[i-1] != '.' )
sprintf( temp, "%s.", capitalize( str ) );
else
sprintf( temp, "%s", capitalize( str ) );
return temp;

}

Not sure if this was the best way to do it... but it comes up with what I want...
USA #13
But I cant seem to get it to check for periond, !, and ? all at the same time. I thought you could just do '.' || '!' but that doesn't work soo I am guessing something I've done is incorrect.
USA #14
if ( str[i-1] != '.' && str[i-1] != '!' && str[i-1] != '?' )

You intend it to be that?
USA #15
Oi, I feel a bit dumb now... thanks. Cant believe I didn't think of that... heh