C or C++ Program for Case Conversion

This article discuss about case conversion in C or C++ and shows the programs that convert from uppercase to lower case in c or C++. Let us discuss about case conversion.

What is Case Conversion?
Case Conversion is a process to convert upper case to lower case or to convert lower case to upper case.
ASCII value of ‘A’ is 65 and  ‘a’ is 97. Difference between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of ‘A’ then it will be ‘a’ and if will we subtract 32 in ASCII value of ‘a’ it will be ‘A’. It is true for all alphabets.

General rules:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32

C or C++ Code for Case Conversion

#include
#include
#include
#include
int main(){
clrscr();
int i,j,n,k;
char *s;

      cout<<" \" This program will convert upper case to lower or vice virsa. \""<<endl<<endl;

      cout<<"What is the string ?. Give a String/Sentence. :"<<endl;
      gets(s);

      k=strlen(s);
      cout<<endl<<"The string that you have given is :"<<endl;
      cout<<s;

      cout<<endl<<"After case conversion it is :"<<endl;

      for(i=0;i<=k;i++)
      {
          n=int(s[i]);    
          if(n==32) {
            s[i]=char(n);
    	    cout<<s[i];     	  }               else if((n>=48) && (n<=57)){
    	    s[i]=char(n);
    	    cout<<s[i];     	  }               else if((n>=97) && (n<=122)) {
    	    n=n-32;
    	    s[i]=char(n);
    	    cout<<s[i];     	  }               else if((n>=65) && (n<=90)) {
    	    n=n+32;
    	    s[i]=char(n);
    	    cout<<s[i];
    	  }
      }

getch();
}

Comments

  1. By JC Rider

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *