I think if you just want to toggle the case of letters in some
, you don't need any special algorithms and sophisticated methods.
In fact, no matter what method you select, you must remember that POINTER is the fastest runner.
After reading all methods submitted above, I should say that the method actually is very simple. Take C# as an example, we can use the following method to toggle the
public static unsafe string ToggleString(string s)
fixed (char* p = s)
for (int i = 0; i < s.Length; i++)
if (p[i] > 96 && p[i] < 123) p[i] -= (char)32;
else if (p[i] > 64 && p[i] < 91) p[i] += (char)32;
Upper case letters are from 65 to 90, and lower case ones are from 97 to 122. So the diff-value between one upper case letter and its lower counterpart is 32!
Let's test it with 1000,0000 loops, run it several times:
Stopwatch watch = new Stopwatch();
for (int i = 0; i < 10000000; i++)
The result is:
The average is 2790, about 3 seconds, how about your results ?