How to: Change Letter Case (upper or lower)

Talk about making games.

How to: Change Letter Case (upper or lower)

Postby Bee-Ant » Wed Jun 30, 2010 5:57 pm

viewtopic.php?f=5&t=6854&p=59790#p59790
This is the function I thought since I know strings.
I rarely mess with GE default functions, so I don't know this is already there or not...

Function to change letters into capital or lower mode.

Function (place this on Global code) :
Code: Select all
char *ToUpper(char str[256])
{
    int i,intc;
    char *stru=malloc(256); //get memory allocation for the pointer
    char tmp[2]; //just for the temporary string
    for(i=0;i<strlen(str);i++)
    {
        intc=(int)str[i]; //get the ASCII number
        if(intc>=97&&intc<=122)intc-=32; //if intc in range of small letters decrease it's ASCII number by 32
        sprintf(tmp,"%s",&intc);
        strcat(stru,tmp); //combine the letters
    }
    return stru;
}
char *ToLower(char str[256])
{
    int i,intc;
    char *strl=malloc(256); //get memory allocation for the pointer
    char tmp[2];
    for(i=0;i<strlen(str);i++)
    {
        intc=(int)str[i]; //get the ASCII number
        if(intc>=65&&intc<=90)intc+=32; //if intc in range of capital letters increase it's ASCII number by 32
        sprintf(tmp,"%s",&intc);
        strcat(strl,tmp);
    }
    return strl;
}


Usage :
Code: Select all
strcpy(text,ToUpper("Bee-ant, DST, Fuzzy"); //the output would be: "BEE-ANT, DST, FUZZY"
strcpy(text,ToLower("Bee-ant, DST, Fuzzy"); //the output would be: "bee-ant, dst, fuzzy"


Limitation :
- Since GE can only handle 256 string length, I just follow it...
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: How to: Change Letter Case (upper or lower)

Postby Hblade » Wed Jun 30, 2010 6:31 pm

AWESOME :D!
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: How to: Change Letter Case (upper or lower)

Postby Bee-Ant » Wed Jun 30, 2010 10:01 pm

Thanks :D
In this function, the method are :
- Get the string
- Split the string into single characters
- Get the ASCII number from each character
- Change the case by edit the number
- Convert back the ASCII number into character
- Combine all characters into a string
- Return the string

The key is the ASCII number, It got so long time for me to convert character into it's ASCII number :cry:
Ah...finally... :P
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: How to: Change Letter Case (upper or lower)

Postby Hblade » Wed Jun 30, 2010 10:18 pm

Thats a lot of work for that :D Good job
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest