4.02.2012

Change case of character

Q. Write a C program to accept a character from user and convert it opposite case.

For example, if user enter character d then it is will convert in upper-case D.

Ans.

/*c program to convert character upper-case to lower case and vice verse*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char ch;
 printf("Enter any character : ");
 scanf("%ch", &ch);
 if(ch>='A' && ch<='Z')
     ch=ch+32;
 else if(ch>'a' && ch<='z')
     ch=ch-32;
 printf("Convert case of character : %c",ch);
 getch();
 return 0;
}

/************************************************************
The output of above program would be:
************************************************************/

Output for change case of entered character C program
Figure: Screen shot for change case of entered character by user


You might also like:
  1. How to identify what is entered by user
  2. How to identify case of character
  3. Read and print string
  4. Reverse all string C program
  5. Reverse all words but not string C program
  6. Reverse only first character of string & add extra character C program
  7. Display string vertical C program
  8. Search the words in string C program
  9. Search the match words in string C program
  10. Toggle the string entered by user C program

5 comments:

  1. Good code..keep it up..

    ReplyDelete
  2. You can also Do toggling by this Like ComPuter-> output : cOMpUTER

    char name[100];
    int loop;
    printf("Enter any Sting: ");
    fgets(name,sizeof(name),stdin);

    for (loop=0; name[loop] !=0; loop++)
    {
    if(name[loop]>='A' && name[loop]<='Z')
    name[loop]=name[loop]+32;
    else if(name[loop]>='a' && name[loop]<='z')
    name[loop]=name[loop]-32;

    }
    printf("\nConvert case of character : %s",name);
    return 0;
    }

    ReplyDelete
    Replies
    1. @Ans Ahmad,

      Nice code for Toggling of string in C
      Here some different code for same program as:

      http://cprogrammingcodes.blogspot.com/2012/07/string-title-case.html

      Delete
  3. In else if checking condition, ch>=='a' should be replaced with ch>'a'

    ReplyDelete
  4. Sir it should be ch>=a not ch>a becoz when I have type a to convert in capital letter it doesn't do thats why I am saying try it I have done
    Myself

    ReplyDelete