Created: 2023-08-16 17:34
Status: #concept
Subject: Programming
Tags: C Library Character ASCII Integer

ctype.h

toupper(ch) and tolower(ch)

Converts ch into an uppercase or lowercase character and returns it to the calling function Expression.

ch = toupper(ch);
ch = tolower(ch);

Converting Cases Without ctype.h

  1. We check if it’s in the alphabet first,
  2. then we minus its lowercase position, to isolate its alphabetical position.
  3. then displace it with the uppercase one to get the uppercase version.
char myToUpper(ch) {
  // if it's in the lowercase alphabet
  if ('a' <= ch && ch <= 'z') { 
    // subtract 97 and add 65 to displace it to the uppercase alphabet
    return ch - 'a' + 'A';
  } else {
    // otherwise, just return it normally because we must not convert it
    return ch; 
  }
}

References