PleaseHow this works
The way this works is by getting the ASCII integer representation of each char with printf
and then adding 32
if upper-to->lower
, or subtracting 32
if lower-to->upper
. Then use printf
again to convert the number back to a char. From 'A' -to-> 'a'
we have a difference of 32 chars.
Using printf
to explain:
$ printf "%d\n" "'a"
97
$ printf "%d\n" "'A"
65
97 - 65 = 32
And this is the working version with examples.
Please note the comments in the code, as they explain the functionalitya lot of stuff:
For me it is fine, since I know I will only pass ASCII chars to it. I
I am using this for some case-insensitive CLI options, for example.
I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine and it is a lot easier to understand the internals like this.