A problem about strings
#1

Hi, I found that if I do cell operations on strings, they cannot be cleared unless you loop through the string and clear cell-by-cell.
pawn Код:
new x[16];
x[0] = '1';
x[1] = '2';
x[2] = '3';
x[3] = '4';
x[4] = '5';
printf("%d %s",strlen(x),x);
x = "";
x[0] = '2';
x[1] = '3';
x[2] = '4';
printf("%d %s",strlen(x),x);
x[0] = '\0';
x[0] = '5';
x[1] = '6';
printf("%d %s",strlen(x),x);
for(new i = 0;i < sizeof(x);i++) x[i] = '\0';
x[0] = '9';
printf("%d %s",strlen(x),x);
Output:
Код:
5 12345
5 23445
5 56445
1 9
As you can see, neither x = "" and x[0] = '\0' can clear the string. It is reasonable for the latter to be unable to do so because it only clear the first cell to fake functions like strlen to think it is cleared, but x = "" should work.

So what's the problem with deleting the string?
Reply
#2

Shouldn't the string x must have an array size?
Reply
#3

Do you mean a cell for storing the string length like those strings in Pascal, or simply the array size?
I have already specified the array size/string length as 16. See the code.
Reply
#4

Ah you already get the lenth by using strlen.
Reply
#5

That's not what I mean, and I'm asking for some ideas to solve the problem (or the reason that it act like this). My English is not very well, so sorry if any of you misunderstand this. I want to blank a string like the one in the code, but I can't do that by either x = "" or x[0] = '\0'. I have to loop through the entire string to clear it.
Let me explain the code again:
pawn Код:
new x[16];
x[0] = '1';
x[1] = '2';
x[2] = '3';
x[3] = '4';
x[4] = '5';
//Expected result of x: "12345", actual result: "12345"
printf("%d %s",strlen(x),x);
x = "";//It should clear the string, but it doesn't.
x[0] = '2';
x[1] = '3';
x[2] = '4';
//Expected result of x: "234", actual result: "23445"
//x[3] is still '4', as set in the above code.
//x[4] is still '5', as set in the above code.
printf("%d %s",strlen(x),x);
x[0] = '\0';//It should clear the string, but it doesn't.
x[0] = '5';
x[1] = '6';
//Expected result of x: "56", actual result: "56445"
//x[2] is still '4', as set in the above code.
//x[3] is still '4', as set in the above code.
//x[4] is still '5', as set in the above code.
printf("%d %s",strlen(x),x);
for(new i = 0;i < sizeof(x);i++) x[i] = '\0';//The string is cleared
x[0] = '9';
//Expected result of x: "9", actual result: "9"
//x[1] to x[4] are all cleared, so it works fine.
printf("%d %s",strlen(x),x);
I don't mean to shorten the string, or I'll be using strdel (it also doesn't work in this case). I use cell operations to the string because I want to copy a part of a string to a temp string, but I finally find it unable to remove the text/data in that temp string. I know I can use strmid or something, but whatever.
Sorry for my bad explanation, I hope you can understand this time.
Reply
#6

Why would you want to clear the entire string? Just end the string once you are done replacing characters.

pawn Код:
new x[16];
    x[0] = '1';
    x[1] = '2';
    x[2] = '3';
    x[3] = '4';
    x[4] = '5';
    //Expected result of x: "5 12345", actual result: "5 12345"
    printf("%d %s",strlen(x),x);
    x[0] = '2';
    x[1] = '3';
    x[2] = '4';
    x[3] = '\0';
    //Expected result of x: "3 234", actual result: "3 234"
    printf("%d %s",strlen(x),x);
Reply
#7

I declare that string to use it inside a loop, which requires the string to be cleared before another loop begins. The loop keep on copying a part of another string to it cell-by-cell, so I don't mean to replace the characters, but clearing the string and store some new text into that string.
Reply
#8

kinda made up a scenario in my head about a dynamic string based on some variables, personally I'd just use another string but I came up with this script if I had to use the same string.

pawn Код:
new x[16];
    for(new i;i<10;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: x[i] = '1';
            default: x[i] = '0';
        }
    }
    print(x);
    x[0] = '\0';
    for(new i;i<5;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: strins(x,"3",i,sizeof(x));
            default: strins(x,"2",i,sizeof(x));
        }
    }
    print(x);
result:
0101010101
23232

not sure if it'd be faster than making all the cells null
Reply
#9

Remember, the last will allways be a NULL.
and why are you using LOOP ?
Reply
#10

The problem is that the string isn't cleared completely. If you use x = "" or x[0] = '\0', only the first cell will be cleared, but you think it is completely empty. If you put a character to the first cell again, the entire string with the original characters stored will show up again, which is my problem.

pawn Код:
new x[16];
x[0] = '1';
x[1] = '2';
x[2] = '3';
x[3] = '4';
x[4] = '5';
x = "";
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[0] = '2';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[1] = '3';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[2] = '4';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
Output:
Код:
(yes it is empty here, making you to think that it is completely cleared.)
2 2 3 4 5 (The problem occurs here, the last 4 cells shouldn't appear, as they should have been removed already)
2 3 3 4 5
2 3 4 4 5
Since it act like this, I am not sure if there is any characters after the NULL character that can cause such problems, I have to loop through the whole string and clear it cell-by-cell.
Anyway I can get the range of the string and copy it by things like strmid instead of copying cell-by-cell, but I want to know what the problem is.

Quote:
Originally Posted by cessil
Посмотреть сообщение
kinda made up a scenario in my head about a dynamic string based on some variables, personally I'd just use another string but I came up with this script if I had to use the same string.

pawn Код:
new x[16];
    for(new i;i<10;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: x[i] = '1';
            default: x[i] = '0';
        }
    }
    print(x);
    x[0] = '\0';
    for(new i;i<5;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: strins(x,"3",i,sizeof(x));
            default: strins(x,"2",i,sizeof(x));
        }
    }
    print(x);
result:
0101010101
23232

not sure if it'd be faster than making all the cells null
Let's do the same thing with a nested loop (more suitable to my actual case):
pawn Код:
new x[16];
for(new j = 1;j < 3;j++)
{
    x[0] = '\0';
    for(new i;i<(10 / j);i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: x[i] = 49 + (j - 1) * 2;//'1' if j = 1, '3' if j = 2
            default: x[i] = 48 + (j - 1) * 2;//'0' if j = 1, '2' if j = 2
        }
    }
    print(x);
}
Output:
Код:
0101010101
2323210101
The output explains it all.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)