Question

Define a function ValidRoom() that passes in a string parameter and returns true if the string...

Define a function ValidRoom() that passes in a string parameter and returns true if the string parameter obeys all rules for a building's room address, else returns false. The rules are: (1) Must be two or three characters long. (2) First two characters must be digits, (3) If third character exists, must be a letter (upper or lowercase).
Examples of strings yielding true: "65", "00", "21L". Examples of strings yielding false: "356", "7", "A23", " 65", "45AB".

Recall some available character functions are: isalpha(), isdigit(), isspace(), toupper(), tolower().

(C++ Please!)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include<string>
#include<ctype.h>

using namespace std;

//Function definition
bool ValidRoom(string room)
{
   int len =room.length();
  
   // checking that length must be two or three characters long
   if( len == 2 || len == 3 )
   {
       // Checking that first two characters must be digits
       if( isdigit(room[0]) && isdigit(room[1]) )
       {
           // Checking that if third character exists, must be a letter (upper or lowercase)
           if(len==3)
           {
               if(isalpha(room[2]))
                   return true;
               else
               return false;  
           }
           else
               return true;
       }
       else
           return false;
   }
   else
       return false;
}


// main method
int main()
{
   // An array of strings to test the function
   string arr[] = {"65", "00", "21L","356", "7", "A23", " 65", "45AB"};
  
   //Looping through the array and calling the ValidRoom method
   for(int i=0;i<8;i++)
   {
       // Displaying the result
       if(ValidRoom(arr[i]))
           cout<<arr[i]<<" is a valid room."<<endl;
       else
           cout<<arr[i]<<" is not a valid room."<<endl;
   }
  
   return 0;
}

Sample Run:

Add a comment
Know the answer?
Add Answer to:
Define a function ValidRoom() that passes in a string parameter and returns true if the string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March","April", "May", "June", "July", "August", "September", days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day number...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day...

  • can you use the isspace() method in the code what do i add to the code...

    can you use the isspace() method in the code what do i add to the code if i want to make sure that extra spaces dont affect the output of the code elect sumatra medium roast VS sumatra medium roast Det dat HETTI CV Surface Providesearch coffee record description to search for at that it was not found in the file the first on has another space in it Table 8-1 Some string testing methods Method Description isalnum() Returns true...

  • Create a C project named login_l06t1. Write and test a function strnclean that takes two strings...

    Create a C project named login_l06t1. Write and test a function strnclean that takes two strings as parameters, target and source. Using the ctype library, copy only the alphabetic characters from source to target, and make the characters lower case. Ex: source: David Brown! target: davidbrown Prototype: void strnclean(char *target, const char *source); How do i do this using the C type library below! int isalnum(int c) Returns non-zero (true) if c is an alphanumeric character, zero otherwise. int isalpha(int...

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “user123@domain.ext”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask,...

    C++ PROGRAMMING Implement a bool function whose header is given below. Inside the function, declare, ask, and get a character from the user. The function will return true if the character the user enters is NOT in either of the words, otherwise it will return false. Keep in mind that uppercase and lowercase letters are not the same. You will need to change both words and the character input by the user to the same case (upper or lower) -...

  • 7.10 Strings Implement the get_num_of_non_WS_characters() function. get_num_of_non_WS_characters() has a string parameter and returns the number of...

    7.10 Strings Implement the get_num_of_non_WS_characters() function. get_num_of_non_WS_characters() has a string parameter and returns the number of characters in the string, excluding all whitespace. Implement the get_num_of_words() function. get_num_of_words() has a string parameter and returns the number of words in the string.

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Define a function in pycharm (isValid) that takes a password as a parameter and returns true...

    Define a function in pycharm (isValid) that takes a password as a parameter and returns true if the password is valid or false if the password is invalid. A valid password must be at least 8 characters long and contains $, _, or @. Invoke the function and print valid or invalid according to the returned Boolean value. Sample, if (isValid(password) == True): print(“Valid”) Or, result = isValid(password) If (result): print(“invalid”)

  • project-8a Write a function named count_letters that takes as a parameter a string and returns a...

    project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT