help... Kidding XD But just some optimization...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static char* CAToLower(char* Char)
{
    for (int i = 0;true;++i)
    {
        if (Char[i]=='\0')
        {
            return Char;
        }
        switch (Char[i])
        {
            case 'A':
                 Char[i]='a'; break;
            case 'B':
                 Char[i]='b'; break;
            case 'C':
                 Char[i]='c'; break;
            case 'D':
                 Char[i]='d'; break;
            case 'E':
                 Char[i]='e'; break;
            case 'F':
                 Char[i]='f'; break;
            case 'G':
                 Char[i]='g'; break;
            case 'H':
                 Char[i]='h'; break;
            case 'I':
                 Char[i]='i'; break;
            case 'J':
                 Char[i]='j'; break;
            case 'K':
                 Char[i]='k'; break;
            case 'L':
                 Char[i]='l'; break;
            case 'M':
                 Char[i]='m'; break;
            case 'N':
                 Char[i]='n'; break;
            case 'O':
                 Char[i]='o'; break;
            case 'P':
                 Char[i]='p'; break;
            case 'Q':
                 Char[i]='q'; break;
            case 'R':
                 Char[i]='r'; break;
            case 'S':
                 Char[i]='s'; break;
            case 'T':
                 Char[i]='t'; break;
            case 'U':
                 Char[i]='u'; break;
            case 'V':
                 Char[i]='v'; break;
            case 'W':
                 Char[i]='w'; break;
            case 'X':
                 Char[i]='x'; break;
            case 'Y':
                 Char[i]='y'; break;
            case 'Z':
                 Char[i]='z'; break;
            default:
                 break;
        }
    }
}


My question is if there is a way to simplify that...
Do I look at an ascII map and check to see if the number literal of any of the char is between a capital and if it is subtract the number between them or is that about as simple as it gets?

(I know there is a function that exists but I want to create the function myself...)
Yes, do this:

look at an ascII map and check to see if the number literal of any of the char is between a capital and if it is subtract the number between them


Note A-Z in ascii is 65-90, and to go to lower case you should add (not subtract) 32 to the ascii value.
Thanks muchly... now I do not need to look at the map : )

so my new code would be...
1
2
3
4
5
6
7
8
9
10
static char* CAToLower(char* Char)
{
    for (int i = 0] ; !Char[i]; i++)
    {
        if (Char[i] >= 65 && Char[i] <= 90)
        {
            Char[i] = Char[i] + 32;
        }
    }
}
of course, for readability, I find it's better to use the actual values you intend to use, rather than magic numbers:

1
2
if(Char[i] >= 'A' && Char[i] <= 'Z')
   Char[i] += 'a' - 'A';
Last edited on
actually... that is genius!
I don't know if it is faster, but you could use bitwise operators: Bitwise OR 0x20 to move chars to uppercase, bitwise AND NOT 0x20 to move chars to lowercase, and bitwise XOR 0x20 to move lowercase letters to uppercase and uppercase letters to lowercase. Of course, you would still need and IF statement that checks if the particular char is an actual letter and not a number, symbol or other.
Topic archived. No new replies allowed.