Raised This Month: $ Target: $400
 0% 

Max characters of a string.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Andu.
Member
Join Date: Oct 2013
Location: Belgravistan
Old 04-03-2015 , 17:13   Max characters of a string.
Reply With Quote #1

How can I go through a "for" from 1 to the maximum number of characters of a string ?
Andu. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-03-2015 , 17:23   Re: Max characters of a string.
Reply With Quote #2

You can either use strlen() to get the length of a string or look for the string termination character, EOS.
__________________
fysiks is offline
Andu.
Member
Join Date: Oct 2013
Location: Belgravistan
Old 04-03-2015 , 17:26   Re: Max characters of a string.
Reply With Quote #3

Yes, but i want it for a sentence, can you explain EOS?
Andu. is offline
mottzi
Veteran Member
Join Date: May 2010
Location: Switzerland
Old 04-03-2015 , 17:30   Re: Max characters of a string.
Reply With Quote #4

You might want to look up how c-strings work, just use google.

http://www.amxmodx.org/doc/index.htm...e%2Fstrlen.htm
mottzi is offline
Send a message via MSN to mottzi
Andu.
Member
Join Date: Oct 2013
Location: Belgravistan
Old 04-03-2015 , 17:34   Re: Max characters of a string.
Reply With Quote #5

static szChat[ 192 ];
read_args( szChat, charsmax( szChat ) );

What's the conditions here to be at the end of string
if() add(szChat, 191, ".");

Last edited by Andu.; 04-03-2015 at 17:34.
Andu. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-03-2015 , 17:38   Re: Max characters of a string.
Reply With Quote #6

Quote:
Originally Posted by Andu. View Post
Yes, but i want it for a sentence
A sentence is a string . . .

Quote:
Originally Posted by Andu. View Post
can you explain EOS?
EOS it is a hard coded value that you can use in AMX Mod X plugins which is used to indicate the end of a string. The actual value of EOS is actually zero but it's better if you use EOS. For example, the string "hello world" would be represented by the following array:

Code:
szMyString[0] = 'h'
szMyString[1] = 'e'
szMyString[2] = 'l'
szMyString[3] = 'l'
szMyString[4] = 'o'
szMyString[5] = ' '
szMyString[6] = 'w'
szMyString[7] = 'o'
szMyString[8] = 'r'
szMyString[9] = 'l'
szMyString[10] = 'd'
szMyString[11] = EOS
Quote:
Originally Posted by Andu. View Post
static szChat[ 192 ];
read_args( szChat, charsmax( szChat ) );

What's the conditions here to be at the end of string
if() add(szChat, 191, ".");
That's probably a bad idea (logically) but it's actually easy if you can guarantee that the string will never fill up the whole array (i.e. have enough space to contain the period):

Code:
szChat[strlen(szChat)] = '.'
szChat[strlen(szChat)+1] = EOS
__________________

Last edited by fysiks; 04-03-2015 at 17:41.
fysiks is offline
Andu.
Member
Join Date: Oct 2013
Location: Belgravistan
Old 04-03-2015 , 17:41   Re: Max characters of a string.
Reply With Quote #7

I understood it but
if(!equal(last character of szChat, ".") add(szChat, 191, ".");
How can i write this condition? "equal(last character of szChat"
Andu. is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-03-2015 , 17:44   Re: Max characters of a string.
Reply With Quote #8

I updated my post to respond to the reply I didn't see until after I posted. But the last character of a string is in the index "strlen(szString)-1".
__________________
fysiks is offline
Decak
Senior Member
Join Date: Sep 2012
Old 04-03-2015 , 17:48   Re: Max characters of a string.
Reply With Quote #9

Code:
new string[6] = "Hello."
if(equal(string[strlen(string)-1], "."))
     // some code, but always true(in this case)
So, first character is 'H'
string[0] = 'H'
Last character is '.'
string[5] = '.'
Or
string[strlen(string)-1] = '.'

Edit: fysiks is faster

Last edited by Decak; 04-03-2015 at 17:48.
Decak is offline
Andu.
Member
Join Date: Oct 2013
Location: Belgravistan
Old 04-03-2015 , 18:08   Re: Max characters of a string.
Reply With Quote #10

I understood it but my string is read because is "register_clcmd("say","HookClCmdSay");" and i can't use szChat[] i can only use szChat
Andu. is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:48.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode