Question

In: Computer Science

Write a program that can check whether or not some string is a valid VUnet ID....

Write a program that can check whether or not some string is a valid VUnet ID. A valid VUnet ID consists of 3 letters followed by 3 digits. The letters in the VUnet ID can be written as lower case letters as well as upper case letters.

Examples of valid VUnet IDs are 'ABC123', 'def456', and 'Ghi789'. An example of an invalid VUnet ID is 'ab123c'.

Define functions to structure your program. That is what this assignment is all about!

Hints:

  • you can get a character from a string by using the member function at:
    • std::string s; std::cin >> s; std::cout << s.at(0);
  • you can convert an int to a string like this:
    • std::string s = std::to_string(42);

Note: Use only basic coding like vector and so one. Do not use any complicated codes.As well explain evey step.

Correct executions of the program are shown below :

Please enter a vunet id: tKn200
The vunet id tKn200 is valid

Please enter a vunet id: tkn2000
error: size incorrect (is 7, should be 6)

Please enter a vunet id: ab22cc
error: letter expected at position 3

Please enter a vunet id: abcdef
error: digit expected at position 4

Solutions

Expert Solution

#include <iostream>

using namespace std;

int main()
{
string s;
cout<<"Please enter a vunet id: ";
cin>>s;
int co=0;
int si=s.size();
if(s.size()==6)
{
while(si>0){
if(co<3){
if( (s.at(co)>='A' && s.at(co)<='Z' ) || (s.at(co)>='a' && s.at(co)<='z' ))
co++;

else{
cout<<"error: letter expected at position "<<co+1;
return 0;
}
  
  
}
else
{
if( s.at(co)>='0' && s.at(co)<='9' )
co++;

else{
cout<<"error: letter expected at position "<<co+1;
return 0 ;
}
  
}
si--;
}
  
}
else{
cout<<"error: size incorrect (is "<< s.size()<<",should be 6)";
return 0;
  
}
cout<<"The vunet id"<< s <<" is valid";

return 0;
}


Related Solutions

Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
C++ function to a string into enum function to a enum into a string check valid...
C++ function to a string into enum function to a enum into a string check valid / loop ( asking a couple of times until answer invaild) For example Enum Fruit ( APPLE, STRAWBERRY, BLUEBERRY, BLACKBERRY) output should be what is your favorit fruit? : strawberry you will have STRAWBERRY. what is your favorite fruite : peach invaild TIA
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
Write a program in C++ to check whether a number is prime or not
Write a program in C++ to check whether a number is prime or not
Write a program to check given expression is valid or not.The expression consists of paranthsis like  [{(...
Write a program to check given expression is valid or not.The expression consists of paranthsis like  [{( if valid then convert into postfix expression and after conversion then evaluate postfix expession(using stack) and do not use build in stack. Use the c++ laguage.
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Phython: Given a string x, write a program to check if its first character is the...
Phython: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Using python: Given a string x, write a program to check if its first character is...
Using python: Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True"
Write a program that inputs a string that represents a binary number. The string can contain...
Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console. Examples of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT