Home > Back-end >  What is the purpose of A_to_a in this string comparison?
What is the purpose of A_to_a in this string comparison?

Time:12-04

somewhere in a code for comparison of two string I saw this:

#define  A_to_a  ('a'-'A')

...

for (i = 0; i < n; i  ) {

    if (!(word[i] == (tmpP -> word[i])) && !(word[i] == (tmpP -> word[i]   A_to_a)) && !(word[i] == (tmpP -> word[i] - A_to_a)) {
        found = 0;
        break;
      }
    }
   
  if (i==n) {
    found = 1;
    break;
  }

I guess it wants to consider uppercase and lower case similr, but how does it work with the phrase A_to_a?

CodePudding user response:

A_to_a is a preprocessor macro. Basically, the text A_to_a in your code will be replaced by ('a'-'A').

Now, 'a' as a numeric value is equal to 97 and correspondingly, 'A' equals 65 as an ASCII value. The difference is 32. This is the delta between any upper and lowercase letters in ASCII. So, something like 't' can be converted to 'T' by subtracting this amount.

CodePudding user response:

'a' - 'A' is a simply an integer number. If you add this number to the upper case letter you will get the lower case letter.

  1. !(word[i] == (tmpP -> word[i])) checks if they are not the same
  2. !(word[i] == (tmpP -> word[i] A_to_a)). tmpP -> word[i] A_to_a converts tmpP -> word[i] to the lower case and the checks if it is not the same as word[i].
  3. !(word[i] == (tmpP -> word[i] - A_to_a)). tmpP -> word[i] - A_to_a converts tmpP -> word[i] to the upper case and the checks if it is not the same as word[i].

If all three form non case sensitive comparison.

But good code should use standard functions toupper or tolower. Above code is simply not good.

CodePudding user response:

This is just a bad code.:)

For example instead of this if statement

if (!(word[i] == (tmpP -> word[i])) && !(word[i] == (tmpP -> word[i]   A_to_a)) && !(word[i] == (tmpP -> word[i] - A_to_a)) {

it will be enough to write

if ( !( toupper( ( unsigned char )word[i] ) == toupper( ( unsigned char )tmpP->word[i] ) ) )

that is more clear and safer.

The macro

#define  A_to_a  ('a'-'A')

serves to convert letters either to upper case or lower case. But it is unsafe and can produce an unexpected result used in the conditions of the original if statement.

For example if you have

char c = 'B';

then to convert this variable to lower case you can write

c - 'A'   'a'

that is equivalent to

'B' - 'A'   'a'

As 'B' - 'A' is equal to 1 then 1 'a' yields 'b'.

The above expression can be written using the macro like

c   A_to_a

that produce

c   'a' - 'A'

that is the same as written above expression

c - 'A'   'a'
  • Related