I have an assignment that I need your help on. This one is about password c-stri
ID: 3715583 • Letter: I
Question
I have an assignment that I need your help on. This one is about password c-strings for C++. Use C++ (Dev-C++ preferred but up to you). Thanks in advance and here is the assignment:
Write a program that uses c-strings to check whether a password that a user enters is a valid password, with the following rules.
Password rules:
The password must be at least 8 characters long.
The password must contain at least:
two alphabetic characters [a-zA-Z]
one numeric character [0-9];
one special character from this set: $ # ? *
The password must not contain spaces.
The password must not begin with a dollar sign [$] or an asterisk [*].
The first 2 characters of the password must not be the same character.
Write a program that asks the user for a password and then verifies that it meets the stated criteria. If it is a valid password, the program should display a message saying the password is valid. If it does not verify, the program should display a message telling the user why the password is invalid. Delegate different tasks to different functions so your code is clear and easy to follow!
Remember you can use the built-in c-string functions so you don't have to recreate them in your code!
For example, this password is valid: pass1*co#234
While this password is invalid for several reasons: notgood1
Explanation / Answer
Please find below the C++ Program as required. Moreover, Comments has been added for better understanding of the program
#include<bits/stdc++.h>
using namespace std;
void check_pass_spaces(char s1[]) // function to check if password contain spaces.
//To check password must not begin with a dollar sign [$] or an asterisk [*].
// To check the first 2 characters of the password must not be the same character.
{
int co =0;
for(int i=0;i<strlen(s1);i++)
{
if(s1[i]==32)
{
co++;
}
}
if(co>0)
{
cout<<"This Password is invalid : The password must not contain spaces.";
}
else
{
if(s1[0]=='$' || s1[0] =='*')
{
cout<<"This Password is invalid : The password must not begin with a dollar sign [$] or an asterisk [*].";
}
else
{
if(s1[0]==s1[1])
{
cout<<"This Password is invalid : The first 2 characters of the password must not be the same character.";
}
else
{
cout<<"This password is valid : "<<s1;;
}
}
}
}
void check_pass_special_char(char s1[]) // function to check if password contain special characters
{
int co =0;
for(int i=0;i<strlen(s1);i++)
{
if(s1[i]=='$' || s1[i]=='#' || s1[i]=='?' || s1[i]=='#')
{
co++;
}
}
if(co>0)
{
check_pass_spaces(s1);
}
else
{
cout<<"This Password is invalid : The password must contain at one special character";
}
}
void check_pass_numeric(char s1[]) // function to check if password contain at least one numeric characters
{
int co =0;
for(int i=0;i<strlen(s1);i++)
{
if(s1[i]>48 && s1[i]<57)
{
co++;
}
}
if(co>1)
{
check_pass_special_char(s1);
}
else
{
cout<<"This Password is invalid : The password must contain at one numeric character";
}
}
void check_pass_alpha(char s1[]) // function to check if password contain at least two alphabetic characters
{
int co =0;
for(int i=0;i<strlen(s1);i++)
{
if((s1[i]>65 && s1[i]<90) || (s1[i]>97 && s1[i]<122))
{
co++;
}
}
if(co>2)
{
check_pass_numeric(s1);
}
else
{
cout<<"This Password is invalid : The password must contain at least:two alphabetic characters [a-zA-Z]";
}
}
void check_pass_len(char s1[]) // function to check password length
{
if(strlen(s1)>8)
{
check_pass_alpha(s1);
}
else
{
cout<<"This Password is invalid : The password must be at least 8 characters long.";
}
}
void check_pass(char s1[])
{
check_pass_len(s1);
}
int main()
{
char s1[100001];
cout<<"Enter Your Password : ";
cin.getline(s1,100001); // to take password input as string
cout<<" ";
check_pass(s1);
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.