C - Changing characters' Case

Introduction

The CTYPE functions toupper() and tolower() can change character case.

Demo

#include <stdio.h> 
#include <ctype.h> 

int main() /*from   ww  w.  jav  a  2s . c  om*/
{ 
   char answer; 

   printf("Would you like to blow up the moon? "); 
   scanf("%c",&answer); 
   answer = toupper(answer); 
   if(answer=='Y') 
       puts("BOOM!"); 
   else 
       puts("The moon is safe"); 
   return(0); 
}

Result

The user is asked to type Y for Yes or N for No.

It uses toupper() to convert the character input to uppercase.

Only a single if condition is required to test for Y or y input.

Related Topic