So I changed my program to take in a cstr and convert it toUpper or toLower.
I was wondering if my algorithms are efficient??? Is there a better way?
They seem efficient to me, two comparisons nested in one loop.
//Michael Ervin - toUpper and toLower functions
#include <iostream>
usingnamespace std;
void toUpper(char* _str);
void toLower(char* _str);
int main()
{
cout << "This program uses an XOR mask to convert\n" <<
"a character toUpper or toLower. The mask is\n" <<
"00100000b or 0x20h. This works because the\n" <<
"5th bit is changed. 1 to 0 for toLower\n" <<
"and 0 to 1 for toUpper.\n" << endl;
cout << "Please enter a string (max length 256)...\n";
char ch[256];
cin.getline(ch,256);
toUpper(ch);
cout << ch << endl;
cout << endl << "Press ENTER to continue...";
cin.get();
return 0;
}
void toUpper(char* _str)
{
int i = 0;
while (_str[i] != '\0')
{
if (_str[i] >= 0x61 && _str[i] <= 0x7A)
(_str[i] = _str[i] ^ 0x20);
i++;
}
}
void toLower(char* _str)
{
int i = 0;
while (_str[i] != '\0')
{
if (_str[i] >= 0x41 && _str[i] <= 0x5A)
(_str[i] = _str[i] ^ 0x20);
i++;
}
}
Rather than using ASCII codes that are difficult to understand, why not use character literals? It makes it much more clear, and there's less room for error:
1 2 3 4 5 6 7 8 9 10
void toUpper(char* _str)
{
int i = 0;
while (_str[i] != '\0')
{
if (_str[i] >= 'a' && _str[i] <= 'z')
_str[i] += 'A' - 'a';
i++;
}
}
Of course there's also a toupper() in the standard library that you could use, but I figured you were doing this as an excersize and not to be practical.
I am using the xor because that was the exercise (and I am now aware that I didn't specify that fact) and I am learning assembly so I am trying to do things more on the bit level, If I used addition and subtraction then that is just more assembly for the compiler to generate(me thinks). Im guessing if one does not understand the ascii codes then they are not going to be able to tell me if there is a more efficient way, as I am assuming that person would be inexperienced.
I have not yet looked at VS 2010 algorithm for toUpper and toLower, I am trying to optimize mine and then compare.
Code clarity goes a long way. Even though that isn't as clear with xors.
The point is: ASCII codes and other magic numbers are hard to understand, confusing and easy to make mistakes with.
If I used addition and subtraction then that is just more assembly for the compiler to generate(me thinks)
Subtraction is 1 instruction, just like XOR. It doesn't make the assembly any more complicated.
The underlying circuitry for subtraction would be more complicated, but unless you're studying binary logic it doesn't really apply.
Im guessing if one does not understand the ascii codes then they are not going to be able to tell me if there is a more efficient way, as I am assuming that person would be inexperienced.
It's not about not being able to understand the code. It's about being able to easily understand the code.
Using raw ASCII codes does nothing but make your code intentionally more confusing, and makes it more work for yourself and others to decipher. Whereas using the character literals is easily identifiable for everyone.