Question

In: Computer Science

Write a subroutine that will receive a char input value. This subroutine should then use a...

Write a subroutine that will receive a char input value. This subroutine should then use a switch statement to determine if the char is a vowel, consonant, digit, or other type of character.

Write the subroutine only,
This is in C++

Solutions

Expert Solution

The C++ code to find the given character is vowel or consonant or special character or a number is:

#include<iostream>
#include<cstring>
using namespace std;

//Determine() function checks if the char ch is
// a vowel or consonant or number or special character
// This function returns nothing
void Determine(char ch){
    //Checking if the char is a number using ascii values
    //ch-0 gives ascii value of character in ch
    //Ascii values of digits are 48 to 57
    if(ch-0>=48 && ch-0<=57){
    ch=1;
    }
    //If ch is not a number or alphabet, then any other ascii value
    //is a special character
    if((ch-0<=48 || ch-0>=57) && (ch-0<=97 || ch-0>=122) && (ch-0<=65 || ch-0>=90)){
        ch=2;
    }
    //Using switch case to determine and print appropriate result
    switch (ch)
    {
    //for case a e i o u printing vowels.
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
    cout<<"Entered character is a vowel"<<endl;
        break;
    //We checked for digits using ascii values and 
    //assigned ch=1 if ch is a digit
    //so for case 1 we need to print its a number
    case 1:
    cout<<"Entered character is a number"<<endl;
    break;
    case 2:
    cout<<"Entered character is a special character"<<endl;
    break;
    default:
    cout<<"Entered character is a Consonant"<<endl;
        break;
    }
}

int main(){
    //Declaring a variable of type char
    //char variables can store only one character
    char ch;
    //Promprint for input and reading a character
    //Remember, if you enter a word instead of char
    //Compiler wont throw a error, instead it stores first char of word
    cout<<"Enter a character: ";
    cin>>ch;
    //Calling Determine() function to determine 
    //if ch is vowel or consonant or other
    Determine(ch);
}

I have used ascii values to check for numbers and speacial characters. Then using a switch case first I checked for a,e,i,o,u cases and printed vowel if true, if false based on previous checks I wrote cases to print if it is a number or special character.

I have provided comments in the program explaining the process and the functions used. I have tested the code for all possible cases and validated the outputs.

I am sharing few output screenshots for your reference.

Let us see what will be the output if we try to enter a word as input:

As you can see, the compiler didn't throw any exception, instead it took the first letter of the word and stored in ch. So we got output as consonant.

Hope this answer helps you.

Thank you.


Related Solutions

Write a subroutine that will receive a char input value. This subroutine should then use a...
Write a subroutine that will receive a char input value. This subroutine should then use a switch statement to determine if the char is a vowel, consonant, digit, or other type of character. Write the subroutine only,
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be passed in on the stack. The answer should be returned in eax. The subroutine should not disturb any registers except eax, ecx, and edx. (Save any registers on the stack and restore them before exiting the subroutine.) Upon entry to the subroutine, push ebp, etc., to access the parameter.
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a function that will receive two input arguments that is the height in inches (in.)...
Write a function that will receive two input arguments that is the height in inches (in.) and the weight in pounds (lb) of a person and return two output arguments that is the height in meters (m) and mass in kilograms (kg). Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. Determine your own height and weight in SI units. Note: 1 meter = 39.3701 inch, 1 foot = 12 inches,...
Use python write a function that translates the input string into Pig Latin. The translation should...
Use python write a function that translates the input string into Pig Latin. The translation should be done word by word, where all words will be separated by only one space. You may assume that each word must have at least one vowel (a,e,i,o,u and uppercased counterparts), and there will be no punctuation or other special characters in the input string. The Pig Latin rules are as follows: For words that begin with consonants, all letters before the initial vowel...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds...
Write in C programming language Write the function replace(char b[], char f[], char t[]). which finds the string 'f' in the string 'b' and replaces it with the string 't'. You can assume that f and t same length Example: char string[] = "zap";     replace(string, "ap", "oo"); --changes 'string' to "zoo".     *don't assume substring being replaced is singular, or that its own substrings are unique.     *Don't re scan letters already checked and replaced         char string[] =...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes...
[ Write in C, not C++] Define a function char* deleteSymbol(char *s, char x) that removes the character x from string s. For s[] = “America”, a call to deleteSymbol(s, ‘a’) converts s[] = “Ame”
Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT