strip function
#1

I'm trying to make a strip function that will strip leading and trailing spaces and extra spaces from within a string, it should function like this:

input:
Код:
 The quick  brown  fox   jumps    over     the      lazy      dog.
ouput:
Код:
The quick brown fox jumps over the lazy dog.
but the function I made is not taking out the 1 leading space before the first non space character.

function:
pawn Код:
stock strip(const s[])
{
    new
        j = 0,
        tmp[256];
    if(isnull(s))
      return tmp;
    for (new i = 0; (s[i] != '\0' && s[i] != '\t'); i++)
    {
        if ((s[i] == ' ') && (s[i+1] == ' ' || s[i+1] == '\0'))
        {
        }
        else
        {
            tmp[j] = s[i];
            j++;
        }
    }
    return tmp;
}
Anyone know whats wrong?
Reply
#2

I read the code, and apparently is everything appropriate, just found it strange here:

pawn Код:
tmp[j] = s[i];
  j++;
What did you do here?

Sorry my bad english.


Reply
#3

it copies over the appropriate characters to the new string which is then returned.
Reply
#4

Returning a copy of the string might not be the best way since it limits the length of strings the function will work on. I'm not sure if this is exactly what you want since you stop at tabs but oh well.

pawn Код:
stock strip(string[])
{
    if(isnull(string)) return;
    if(ispacked(string)) {
        for(new i; string{i} != '\0'; i++) {
            if(string{i} <= ' ') {
                new end = i;
                while('\0' < string{end + 1} <= ' ') end++;
                if(i == 0) {
                    strdel(string, i, end + 1);
                } else if(string{end + 1} == '\0') {
                    strdel(string, i, end + 1);
                    return;
                } else if(i != end) {
                    strdel(string, i, end);
                }
            }
        }
    } else {
        for(new i; string[i] != '\0'; i++) {
            if(string[i] <= ' ') {
                new end = i;
                while('\0' < string[end + 1] <= ' ') end++;
                if(i == 0) {
                    strdel(string, i, end + 1);
                } else if(string[end + 1] == '\0') {
                    strdel(string, i, end + 1);
                    return;
                } else if(i != end) {
                    strdel(string, i, end);
                }
            }
        }
    }
}
Reply
#5

Thanks Dabomber, I've edited and optimized that code a little, and it works great.

pawn Код:
stock strip(s[])
{
    if (isnull(s))
        return 0;
    for (new i = 0; (s[i] != '\0' && s[i] != '\t' && s[i+1] != '\0'); i++)
    {
        if(s[i] <= ' ')
        {
            new
                end = i;
            while ('\0' < s[end + 1] <= ' ')
                end++;
            if(!i)
                strdel(s, i, end + 1);
            else if(s[end + 1] == '\0')
                strdel(s, i, end + 1);
            else if(i != end)
                strdel(s, i, end);
        }
    }
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)