Problem Statement
Take a word and the check if the entered word has at least one vowel, one digit, and one uppercase letter, and its length does not exceed 8 characters.
Concepts and Explanation
This program checks if the entered word string contains at least one vowel, one digit, and one uppercase letter, and if its length does not exceed 8 characters.
Header files: #include <iostream>
The iostream
header file is part of the C++ standard library, providing functionality for input and output streams. In this program, we use this header file.
Main Function
The main()
function is the entry point of the program, where the code execution starts. It has a return type of int
.
Variables
We declare a string variable named word
to store the user's input word.
Console Input and Output
We use cout
to print a message to the console, asking the user to enter a word. We use getline()
to read in the whole line of text entered by the user and store it in the word
variable.
Checking for Vowel, Digit, and Uppercase Letter
We use a for
loop to iterate over every character in the word
string. For each character, we check if it is a vowel, digit, or uppercase letter using the isupper()
, isdigit()
, and strchr()
functions from the cctype
and cstring
header files. If any of these conditions are met, we set a boolean flag variable named has_vowel_digit_uppercase
to true
.
Checking for Length
We use the size()
function to get the length of the word
string. If the length is less than or equal to 8 characters, we set another boolean flag variable named has_valid_length
to true
.
Outputting the Result
We use if
statements to check the values of the two boolean flag variables. If both are true
, we output a message to the user saying that the word is valid. Otherwise, we output a message saying that the word is invalid and list the specific requirements that it failed to meet.
Ending the Program
The program ends by returning 0
, indicating that the program has executed successfully.
Method 1: Simplest Method
Code
/*
* -----------------------------------------------------------
* OOPS using C++ Language (CSU1287P)
* Instructor: Dr. Pankaj Vaidya | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <iostream>
#include <string>
using namespace std;
void checkword(string word)
{
int vowels = 0, digits = 0, uppercase = 0, length = 0;
for (char c : word) // for loop with variable : iterable combination
{
if (c >= 'A' && c <= 'Z')
{
uppercase++;
}
else if (c >= '0' && c <= '9')
{
digits++;
}
else if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
vowels++;
}
length++;
}
if (vowels >= 1 && digits >= 1 && uppercase >= 1 && length <= 8)
{
cout << "\t The word \"" << word << "\" meets the criteria." << endl;
}
else
{
cout << "\t The word \"" << word << "\" does not meet the criteria." << endl;
}
}
int main()
{
string word;
cout << "This program checks if the entered word has at least one vowel, one digit, and one uppercase letter, and its length does not exceed 8 characters";
cout << "Enter a word: ";
cin >> word;
checkword(word);
return 0;
}
Method 2: Using seperate loops
#include <iostream>
#include <string>
using namespace std;
bool checkWord(string word) {
int vowels = 0, digits = 0, uppercase = 0;
for (char c : word) {
if (c >= 'A' && c <= 'Z') {
uppercase++;
} else if (c >= '0' && c <= '9') {
digits++;
} else if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
vowels++;
}
}
int length = word.length();
if (vowels >= 1 && digits >= 1 && uppercase >= 1 && length <= 8) {
return true;
} else {
return false;
}
}
int main() {
string word;
cout << "Enter a word: ";
cin >> word;
if (checkWord(word)) {
cout << "The word \"" << word << "\" meets the criteria." << endl;
} else {
cout << "The word \"" << word << "\" does not meet the criteria." << endl;
}
return 0;
}
Method 3: Using regex
#include <iostream>
#include <string>
#include <regex>
using namespace std;
bool checkWord(string word) {
regex pattern("[AEIOUaeiou][0-9][A-Z].{0,5}");
return regex_match(word, pattern);
}
int main() {
string word;
cout << "Enter a word: ";
cin >> word;
if (checkWord(word)) {
cout << "The word \"" << word << "\" meets the criteria." << endl;
} else {
cout << "The word \"" << word << "\" does not meet the criteria." << endl;
}
return 0;
}
Output for Method 1:
This program checks if the entered word has at least one vowel, one digit, and one uppercase letter, and its length does not exceed 8 characters
Enter a word: Test3est
The word "Test3est" meets the criteria."