# Answer to Question #55461 in C for himanshu

Question #55461

Write a function boolean isValidPassword (String password)

A Password is valid if it satisfies following conditions:

1. Length of the password must be more than 10(without counting spaces).

2. Password must contain at least one capital and one small alphabet

3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition.

4. Password must contain at least one special character from the given set-

{$, @, _, -, ., /}

A Password is valid if it satisfies following conditions:

1. Length of the password must be more than 10(without counting spaces).

2. Password must contain at least one capital and one small alphabet

3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition.

4. Password must contain at least one special character from the given set-

{$, @, _, -, ., /}

Expert's answer

Solve

#include <iostream>

#include <string>

using namespace std;

bool isValidPassword(string pass)

{

//1. Length of the password must be more than 10(without counting spaces)

int i,k=0;

int kCapital=0,kSmall=0,kDigit=0,kSpec=0;

for(i=0;i<pass.length();i++)

{

if(pass[i]!=' ')

k++;

if((pass[i]>='a')&&(pass[i]<='z'))

kSmall++;

if((pass[i]>='A')&&(pass[i]<='Z'))

kCapital++;

if((pass[i]>='0')&&(pass[i]<='9'))

kDigit++;

//$, @, _, -, ., /

if((pass[i]=='$')||(pass[i]=='@')||(pass[i]=='_')||(pass[i]=='-')

||(pass[i]=='.')||(pass[i]=='/'))

kSpec++;

}

if(k<=10)

return false;

//2. Password must contain at least one capital and one small alphabet

if((kCapital==0)||(kSmall==0))

return false;

//3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition.

if(kDigit<2)

if(kSpec==0)

return false;

return true;

}

int main()

{

string pass;

cout<<"Input password: ";

getline(cin,pass);

if(isValidPassword(pass))

cout<<"\nValid password";

else

cout<<"\nInvalid password";

return 0;

}

#include <iostream>

#include <string>

using namespace std;

bool isValidPassword(string pass)

{

//1. Length of the password must be more than 10(without counting spaces)

int i,k=0;

int kCapital=0,kSmall=0,kDigit=0,kSpec=0;

for(i=0;i<pass.length();i++)

{

if(pass[i]!=' ')

k++;

if((pass[i]>='a')&&(pass[i]<='z'))

kSmall++;

if((pass[i]>='A')&&(pass[i]<='Z'))

kCapital++;

if((pass[i]>='0')&&(pass[i]<='9'))

kDigit++;

//$, @, _, -, ., /

if((pass[i]=='$')||(pass[i]=='@')||(pass[i]=='_')||(pass[i]=='-')

||(pass[i]=='.')||(pass[i]=='/'))

kSpec++;

}

if(k<=10)

return false;

//2. Password must contain at least one capital and one small alphabet

if((kCapital==0)||(kSmall==0))

return false;

//3. Password must contain at least 2 digits and if it does not have 2 digits then see next condition.

if(kDigit<2)

if(kSpec==0)

return false;

return true;

}

int main()

{

string pass;

cout<<"Input password: ";

getline(cin,pass);

if(isValidPassword(pass))

cout<<"\nValid password";

else

cout<<"\nInvalid password";

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment