Object

Write a program to input a character. If this character is in uppercase then convert it into lowercase and vice versa (without using toupper/tolower or isupper/islower functions).

Algorithm

  1. Declare integer variable a and character variable c.
  2. Read the character c.
  3. Set a = c (i.e., convert character into integer).
  4. IF (a >= 65 && a <= 90), then
    • a = a + 32.
    • Write the character a.
    • ELSE
      • a = a – 32.
    • Write the character a.
    • [End of IF-ELSE structure.]
  5. Exit.

Flowchart

C++ Source Code

// program 22a
#include <iostream>
using namespace std;

int main()
{
  int a;
  char c;
  cout << "Enter a character: ";
  cin >> c;
  a = c;
  
  if (a >= 65 && a <= 90)
  {
    a = a + 32;
    cout << "The other case is " << static_cast<char>(a);
  }
  else
  {
    a = a - 32;
    cout << "The other case is " << static_cast<char>(a);
  }
  return 0;
}

Object (second method)

Write a program to input a character. If this character is in uppercase then convert it into lowercase and vice versa.

C++ Source Code

// program 22b
#include <iostream>
using namespace std;

int main()
{
  char c;
  cout << "Enter a character: ";
  cin >> c;

  if (isupper(c))
  {
    c = tolower(c);
    cout << "The other case is " << c;
  }
  else if (islower(c))
  {
    c = toupper(c);
    cout << "The other case is " << c;
  }
  else
  {
    cout << "Invalid input! It's not an alphabet.";
  }
  return 0;
}

C Source Code

/*program 22*/
#include<stdio.h>

int main()
{
  int a;
  char c;
  printf("Enter a character: ");
  scanf("%c", &c);
  a=c;
  if(a>=65 && a<=90)
     {
     a=a+32;
     printf("The other case is %c", a);
     }
  else
     {
     a=a-32;
     printf("The other case is %c", a);
     }
  return 0;
}

Output

Enter a character: p
The other case is P

Design a site like this with WordPress.com
Get started