// Demonstrating how to play with characters in C // Program asks the user for a letter, checks if the letter is upper or lower // case and then converts it to the opposite case // Week 2, Lecture 3 // Problem: A user inputs a letter, we must check // if the letter is upper or lower case and then // output the same letter, but in the opposite case // Possibilities/Breakpoints? // 1. Scan in a letter (scanf()) // 2. Check if upper or lower case - if it is between a and z lower case; A and Z // then it must be upper case // a <= character <= z // character >= 'a' && character <= 'z' // character >= 'A' && character <= 'Z' // 3. If lower - then convert to an upper // 4. If upper - then convert to a lower #include int main(void){ // Declare a variable of type char to scan in a letter into char character; // Initialise it by scanning into it printf("Enter a character: "); // Declare a variable that will store the output of scanf() int scanf_return; scanf_return = scanf(" %c", &character); if (scanf_return == 1){ if (character >= 'a' && character <= 'z'){ character = character - 'a' + 'A'; printf("It is lower case, the new letter is: %c\n", character); // I need to convert it to an upper case // Let's say my input is 'a' = 97 // that means I want it to become 'A' = 65 } else if (character >= 'A' && character <= 'Z') { character = character - 'A' + 'a'; printf("It is upper case. the new character is %c\n", character); } else { printf("You did not enter a letter."); } } return 0; }