AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Loop Through Characters in String (https://forums.alliedmods.net/showthread.php?t=111051)

Bigbuck 12-06-2009 10:37

Loop Through Characters in String
 
Could anyone provide an example on how to loop through each character in a string? What I need to do is check if the letter is upper case and then change it to lower case or if it is a space, remove it. I know how to do the change the characters, but I am not sure how to actually get to each character.

meng 12-06-2009 19:22

Re: Loop Through Characters in String
 
PHP Code:

    new len strlen(mystring);
    for (new 
0<= leni++)
    {
        if (
mystring[i/* your conditions */)
        {
             
// your adjustments
        
}
    } 


berni 12-06-2009 20:03

Re: Loop Through Characters in String
 
A more efficient way:
PHP Code:

new n=0;
while (
mystring[n] != '\0') {
    
     
// Do your stuff here...

    
n++;



berni 12-06-2009 20:15

Re: Loop Through Characters in String
 
A complete function:

PHP Code:

stock StrToLowerRemoveBlanks(const String:str[], String:buffer[], bufsize) {

    new 
n=0x=0;
    while (
str[n] != '\0' && < (bufsize-1)) { // Make sure we are inside bounds

        
new char str[n++]; // Caching
    
        
if (char == ' ') { // Am I nothing ?
            // Let's do nothing !
            
continue;
        }
        else if (
IsCharUpper(char)) { // Am I big ?
            
char CharToLower(char); // Big becomes low
        
}

        
buffer[x++] = char// Write into our new string
    
}

    
buffer[x++] = '\0'// Finalize with the end ( = always 0 for strings)

    
return x// return number of bytes written for later proove


but not tested ;)

Bigbuck 12-06-2009 20:25

Re: Loop Through Characters in String
 
Would it be something like this?

PHP Code:

new 0;
while (
gamemode[n] != '\0'
{
     if (
IsCharUpper(n))
     {
         
CharToLower(n);
     }
     if (
IsCharSpace(n))
     {
         
//do what?
     
}

     
n++;



berni 12-06-2009 21:43

Re: Loop Through Characters in String
 
No it's something like I wrote.


All times are GMT -4. The time now is 14:52.

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