C Program to classify character input

This program asks the user to enter a single character of text. Then it examines the ASCII code to determine whether it is a lowercase, uppercase or a digit. It also shows how a loop can be implemented with statement labels.

Last Reviewed and Updated on May 17, 2017
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

Advertisement

Program to classify character input.

Write a program that asks the user to enter a character. Implement the following algorithm. Don't use loops. You can use labels.

  1. User enters a character.
  2. If he enters a lowercase English character display "lowercase".
  3. If he enters an uppercase English character display "uppercase".
  4. If the character is a numeric digit display "It's a number".
  5. If he enters something else, display "Something else".
  6. If the user didn't enter $, repeat the above steps. Don't use loops.
  7. If the user entered $, your program should exit.
int main()
{

    char c;

    // label this statement so that we can
    // come back to it if required
lblX:
    cout << "Enter a char:";

    cin >> c;

    if(c >= 'a' && c <= 'z')
    {

        cout << "Lowercase" << endl;

    }

    else if (c >= 'A' && c <= 'Z')
    {

        cout << "Uppercase" << endl;

    }

    else if(c >= '0' && c <= '9')
    {

        cout << "It's a digit";

    }

    else
    {

        cout << "Something else";

    }

    cout << endl;

    if(c != '$')
    {

        goto lblX;

    }

    cout << "Quitting..." << endl;

    return 0;

}


My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.



Creative Commons License
This Blog Post/Article "C Program to classify character input" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2017-05-17.


Advertisement