In: Computer Science
You've been hired by Avuncular Addresses to write a C++ console application that analyzes and checks a postal address. Prompt for and get from the user an address. Use function getline so that the address can contain spaces. Loop through the address and count the following types of characters:
● Digits (0-9)
● Alphabetic (A-Z, a-z)
● Other
Use function length to control the loop. Use functions isdigit and isalpha to determine the character types. Use formatted output manipulators (setw, left/right) to print the following rows:
● Address
● String length
● Number of digits
● Number of alphas
● Number of other characters
And two columns:
● A left-justified label.
● A right-justified value.
Then test the number of digits and number of alphas. If digits is less than two or alphas is less than three, print an invalid address message. Otherwise, print a valid address message. Define constants for the minimum number of digits and alphas, and the column widths. The output should look like this for invalid and valid input:
Welcome to Avuncular Addresses
------------------------------
Enter an address: 1 West Liberty St
Address: 1 West Liberty St
Length: 16
Digits: 1
Alphas: 12
Other: 3
Address is invalid!
End of Avuncular Addresses
Welcome to Avuncular Addresses
------------------------------
Enter an address: 110 Main St
Address: 110 Main St
Length: 11
Digits: 3
Alphas: 6
Other: 2
Address is valid!
End of Avuncular Addresses
Code:
#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
int main()
{
string address;
int num=0,i,alpha=0,other=0;/*Declaring
variables*/
cout<<"Welcome to Avuncular
Addresses"<<endl;
cout<<"------------------------------"<<endl;
cout<<"Enter an address:";
getline(cin,address);/*Reading input from the
user*/
cout<<"Address:"<<setw(15)<<address<<endl;/*Printing
the address*/
cout<<"Length:
\t"<<address.length()<<endl;/*Length of the
address*/
for(i=0;i<address.length();i++)
{
if(isdigit(address[i]))
{/*If digit increse the coutn of
num*/
num++;
}
else if(isalpha(address[i]))
{/*If alphabet increase the cout of
alpha*/
alpha++;
}
else
{/*Else increase other*/
other++;
}
}
cout<<"Digits: \t"<<num<<endl;
cout<<"Alphas:
\t"<<alpha<<endl;
cout<<"Others:
\t"<<other<<endl; /*Printing the digits
alphabets and others*/
if(num<2 || alpha<3)
{/*If digit is less than 2 or alpha is less than
3*/
cout<<"Address is
invalid!"<<endl;
}
else
{/*else*/
cout<<"Address is
valid!"<<endl;
}
cout<<"End of Avuncular
Addresses"<<endl;
}
Output:
Indentation: