Raised This Month: $ Target: $400
 0% 

Loop Through Characters in String


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Bigbuck
Senior Member
Join Date: Jul 2009
Old 12-06-2009 , 10:37   Loop Through Characters in String
Reply With Quote #1

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.
Bigbuck is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 12-06-2009 , 19:22   Re: Loop Through Characters in String
Reply With Quote #2

PHP Code:
    new len strlen(mystring);
    for (new 
0<= leni++)
    {
        if (
mystring[i/* your conditions */)
        {
             
// your adjustments
        
}
    } 
__________________
.
[ 1 Dumerils Boa | 1 Cali King ]...
.
I'm a lil' spirituous.
meng is offline
Send a message via Yahoo to meng
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 12-06-2009 , 20:03   Re: Loop Through Characters in String
Reply With Quote #3

A more efficient way:
PHP Code:
new n=0;
while (
mystring[n] != '\0') {
    
     
// Do your stuff here...

    
n++;

__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 12-06-2009 , 20:15   Re: Loop Through Characters in String
Reply With Quote #4

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 ;)
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0

Last edited by berni; 12-06-2009 at 20:34.
berni is offline
Bigbuck
Senior Member
Join Date: Jul 2009
Old 12-06-2009 , 20:25   Re: Loop Through Characters in String
Reply With Quote #5

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++;

Bigbuck is offline
berni
SourceMod Plugin Approver
Join Date: May 2007
Location: Austria
Old 12-06-2009 , 21:43   Re: Loop Through Characters in String
Reply With Quote #6

No it's something like I wrote.
__________________
Why reinvent the wheel ? Download smlib with over 350 useful functions.

When people ask me "Plz" just because it's shorter than "Please" I feel perfectly justified to answer "No" because it's shorter than "Yes"
powered by Core i7 3770k | 32GB DDR3 1886Mhz | 2x Vertex4 SSD Raid0
berni is offline
Reply



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

Forum Jump


All times are GMT -4. The time now is 18:17.


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