Welcome, %1$s. Please login or register.

February 21, 2018, 05:09:13 PM
: 1
: Change the case!  ( 1926 )
« : May 18, 2007, 11:52:04 PM From Prateek»



Change the case!


Write an efficient macro to change the case (uppercase to lowercase and lowercase to uppercase) of an alphabet.

 
Liked It? Share it!

              


« #1 : May 19, 2007, 12:01:53 AM From Poonam»

Use isAlpha(), isLower(), is isUpper(), toLower(), toUpper() from ctype.h.
Using a library is always better.
« #2 : May 19, 2007, 11:37:15 AM From Ria»

Using the library APIs is useful as we need not re-write code. However just using very few APIs of a library might turn out to be inefficient as the executable will include the whole library in which we will be accessing only 2 to 3 calls. (Unless the library is in the form of dll). This will increase the size of the executable and sometimes harm performance.

The problem is more glaring in this example. Instead of using ctype.h for converting cases a more efficient macro can be written.


If we observe the ascii values of upper case and lower case letter carefully it can be seen that they differ by 32. Hence subtracting 32 from a lower case letter will yeild an upper case and adding 32 to an upper case will give a lower case letter's ascii value.
« #3 : May 21, 2007, 12:55:25 AM From Sarbartha Sengupta»

#include<stdio.h>
#include<string.h>
main()
{
 char a[20];
 scanf("%s",a);
 int l=strlen(a);
 for(int i=0;i<l;i++)
   {
    if((int)a>64 && (int)a<91)
      {
       a=(char)((int)a+32);
      }
   else if((int)a>96 && (int)a<123)
      {
       a=(char)((int)a-32);
      }

   }
 printf("%s",a);
}
======================================
this programme will solve the problem.
: 1
« previous next »

 

Best RatedList All>>



Latest
Random



SMF 2.0.10 | SMF © 2015, Simple Machines | Contact Webmaster | OnlineFunDb.com © 2009/10 | Legal Disclaimer