Raised This Month: $ Target: $400
 0% 

Counting ^n


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Flipper_SPb
Senior Member
Join Date: Jun 2009
Location: Worldspawn
Old 07-13-2011 , 10:23   Counting ^n
Reply With Quote #1

How to count ^n symbols in the string?
__________________
Полный похуизм.
Flipper_SPb is offline
Send a message via ICQ to Flipper_SPb
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-13-2011 , 10:46   Re: Counting ^n
Reply With Quote #2

Code:
stock str_count(const haystack[], const needle[], ignorecase=0) {     new c     new old, pos, len = strlen(needle)     while((pos += (ignorecase ? containi(haystack[old], needle) : contain(haystack[old], needle))) >= old)     {         c++         old = pos + len     }     return c }

Code:
new count = str_count(string, "^n")
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Flipper_SPb
Senior Member
Join Date: Jun 2009
Location: Worldspawn
Old 07-13-2011 , 11:31   Re: Counting ^n
Reply With Quote #3

Thank you, Exolent
__________________
Полный похуизм.
Flipper_SPb is offline
Send a message via ICQ to Flipper_SPb
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 07-13-2011 , 13:14   Re: Counting ^n
Reply With Quote #4

Here's same but for character counting, without natives inside loop.
Code:
stock char_count( const hayshack[ ], const needle ) {     new iCount, i, iLen = strlen( hayshack );         while( i++ < iLen )     {         if( hayshack[ i ] == needle )         {             iCount++;         }     }         return iCount; }

Code:
new count = char_count( string, '^n' );
__________________
xPaw is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-13-2011 , 13:33   Re: Counting ^n
Reply With Quote #5

Quote:
Originally Posted by xPaw View Post
without natives inside loop.
Code:
iLen = strlen( hayshack );
Liar.

EDIT:

I would do this:

Code:
stock char_count( const hayshack[ ], const needle ) {     new iCount, i = -1, iChar;         while( ( iChar = hayshack[ ++i ] ) )     {         if( iChar == needle )         {             iCount++;         }     }         return iCount; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Flipper_SPb
Senior Member
Join Date: Jun 2009
Location: Worldspawn
Old 07-13-2011 , 13:53   Re: Counting ^n
Reply With Quote #6

I made that:

PHP Code:
new iccount_n

while((my_string[i++]))
        
count_n += _:(== 10
__________________
Полный похуизм.
Flipper_SPb is offline
Send a message via ICQ to Flipper_SPb
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 07-13-2011 , 14:25   Re: Counting ^n
Reply With Quote #7

Quote:
Originally Posted by Exolent[jNr] View Post
Liar.
I said INSIDE loop ;)
Both ways are fine.
__________________
xPaw 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 12:13.


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