SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 30/12/2015, 04:09 PM   #1
VincenzoDrift
Little Clucker
 
VincenzoDrift's Avatar
 
Join Date: Jun 2014
Posts: 47
Reputation: 3
Default how many times a string is repeated in another string

Hi, i have a problem. I need to know how many times a string is repeated in another string.

Example:
Code:
new string[128], string2[128];
string = "No/nNo/nNo";
string2= "No";
I need to know how many times the string "No"(string2) is repeated in "No/nNo/nNo"(string1).
The result shoud be 3 times. What function can I use?

P.S. Sorry for my bad english
__________________
Sorry for my bad english

Last edited by VincenzoDrift; 31/12/2015 at 09:49 AM.
VincenzoDrift is offline   Reply With Quote
Old 30/12/2015, 04:14 PM   #2
Jefff
Banned
 
Join Date: Dec 2007
Posts: 2,593
Reputation: 326
Default Re: how many times a string is repeated in another string

while + strfind "No"
Jefff is offline   Reply With Quote
Old 31/12/2015, 09:46 AM   #3
VincenzoDrift
Little Clucker
 
VincenzoDrift's Avatar
 
Join Date: Jun 2014
Posts: 47
Reputation: 3
Default Re: how many times a string is repeated in another string

Thanks i try to do this, but don't work.
Code:
stock GetQuantityStringInString(haystack[], needle[])
{
	new pos,
		result,
		count;
	for(;;)  //if i use while(true) it give me a warning
	{
	    result = strfind(haystack, needle, false, pos);
	    pos += result;
	    if(result == -1)
	    {
	        break;
	    }
	    else
		{
            count++;
		}
	}
	return count;
}
Any suggestions?
__________________
Sorry for my bad english
VincenzoDrift is offline   Reply With Quote
Old 31/12/2015, 10:18 AM   #4
BroZeus
High-roller
 
BroZeus's Avatar
 
Join Date: Jan 2014
Location: India
Posts: 1,398
Reputation: 173
Default Re: how many times a string is repeated in another string

Use this :
PHP Code:
stock GetQuantityStringInString(haystack[], needle[], ignorecase true)
{
    new 
temp[30], 0;
    for(new 
0strlen(haystack)-strlen(needle); <= l; ++i)
    {
        
strmid(temphaystackii+strlen(needle));
        if(!
strcmp(needletempignorecase))c++;
    }
    return 
c;

__________________


"Be the change that you wish to see in the world."

Check out my Github profile!
BroZeus is offline   Reply With Quote
Old 31/12/2015, 10:42 AM   #5
VincenzoDrift
Little Clucker
 
VincenzoDrift's Avatar
 
Join Date: Jun 2014
Posts: 47
Reputation: 3
Default Re: how many times a string is repeated in another string

Thanks it works, but it give me a warning "tag mismatch" on this line.
Code:
if(!strcmp(needle, temp, ignorecase))c++;
How fix?
__________________
Sorry for my bad english
VincenzoDrift is offline   Reply With Quote
Old 31/12/2015, 10:49 AM   #6
BroZeus
High-roller
 
BroZeus's Avatar
 
Join Date: Jan 2014
Location: India
Posts: 1,398
Reputation: 173
Default Re: how many times a string is repeated in another string

Ah change the function prototype(function header) to this:
Quote:
stock GetQuantityStringInString(haystack[], needle[], bool:ignorecase = true)
Add that bold part
__________________


"Be the change that you wish to see in the world."

Check out my Github profile!
BroZeus is offline   Reply With Quote
Old 31/12/2015, 12:23 PM   #7
Kaliber
High-roller
 
Kaliber's Avatar
 
Join Date: Jun 2012
Location: Localhost
Posts: 1,092
Reputation: 156
Default Re: how many times a string is repeated in another string

The fastest way to do this, is:

PHP Code:
stock static countStrings(const string[], const needle[])
{
    new 
c;
    for(new 
i=strfind(string,needle,true); i!=-1i=strfind(string,needle,true,i+1)) c++;
    return 
c;

Speed Results against your function:

Code:
Your time with 100.000x: 3818ms
My time with 100.000x: 1466ms
Greekz
__________________
Kaliber is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How many times a string is repeated in another string VincenzoDrift Server Support 0 30/12/2015 03:34 PM
[Ajuda] error 037: invalid string (possibly non-terminated string) Tootamorine Português/Portuguese 5 01/03/2015 07:00 PM
String placed two times? ConnorHunter Scripting Help 12 23/05/2014 07:21 PM
string too small. but i dont wanna make the string like 9999.. niels44 Scripting Help 2 23/10/2012 05:36 PM
input line too long (after substitutions) - invalid string (possibly non-terminated string) MahmutBey Help Archive 6 10/03/2011 01:39 PM


All times are GMT. The time now is 04:55 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.