Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ SMAUG
➜ SMAUG coding
➜ Making something like capitalize
Making something like capitalize
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1 2
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Fri 26 May 2006 12:31 AM (UTC) |
Message
| 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.
|
Everything turns around in the end | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #1 on Fri 26 May 2006 01:57 AM (UTC) |
Message
| 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. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #2 on Fri 26 May 2006 02:56 AM (UTC) |
Message
| saddly, I have no idea what you said heh. |
Everything turns around in the end | Top |
|
Posted by
| Zeno
USA (2,871 posts) Bio
|
Date
| Reply #3 on Fri 26 May 2006 03:04 AM (UTC) |
Message
| Get the length of the string, then use an ifcheck to check the last character in the string for whatever you need. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #4 on Fri 26 May 2006 03:17 AM (UTC) |
Message
| 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? |
Everything turns around in the end | Top |
|
Posted by
| Zeno
USA (2,871 posts) Bio
|
Date
| Reply #5 on Fri 26 May 2006 03:21 AM (UTC) |
Message
| 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. |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | Top |
|
Posted by
| Tzaro
USA (41 posts) Bio
|
Date
| Reply #6 on Fri 26 May 2006 04:37 AM (UTC) |
Message
| the function is called "strlen()" :)
|
Implementer of Lost Prophecy,
Tzaro | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #7 on Fri 26 May 2006 09:51 AM (UTC) |
Message
| Now to find out how to use it... |
Everything turns around in the end | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #8 on Fri 26 May 2006 09:53 AM (UTC) |
Message
| 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). |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #9 on Fri 26 May 2006 10:24 PM (UTC) |
Message
| 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 |
Everything turns around in the end | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #10 on Sat 27 May 2006 07:54 AM (UTC) |
Message
| ok so strlen, gives the lenth of a string, but I still dont know how to use that in anyway <.< |
Everything turns around in the end | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #11 on Sat 27 May 2006 09:25 AM (UTC) |
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 |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #12 on Sat 27 May 2006 10:52 AM (UTC) |
Message
| 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... |
Everything turns around in the end | Top |
|
Posted by
| Metsuro
USA (389 posts) Bio
|
Date
| Reply #13 on Sat 27 May 2006 10:58 AM (UTC) |
Message
| 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. |
Everything turns around in the end | Top |
|
Posted by
| Zeno
USA (2,871 posts) Bio
|
Date
| Reply #14 on Sat 27 May 2006 06:53 PM (UTC) |
Message
| if ( str[i-1] != '.' && str[i-1] != '!' && str[i-1] != '?' )
You intend it to be that? |
Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
49,199 views.
This is page 1, subject is 2 pages long: 1 2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top