how many times a string is repeated in another string
#1

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
Reply
#2

while + strfind "No"
Reply
#3

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?
Reply
#4

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;

Reply
#5

Thanks it works, but it give me a warning "tag mismatch" on this line.
Code:
if(!strcmp(needle, temp, ignorecase))c++;
How fix?
Reply
#6

Ah change the function prototype(function header) to this:
Quote:

stock GetQuantityStringInString(haystack[], needle[], bool:ignorecase = true)

Add that bold part
Reply
#7

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)