6.0k views
1 vote
Write a function named "isValidPassword" that takes in a string and returns 1 if it stores a valid password and 0 otherwise, A valid password must contain at least one lowercase letter, one uppercase letter, and at least one nonletter character. Example: if input string is Example: if input string is "Pass_word", function should return "1" "password", function should return "0"

1 Answer

2 votes

Answer:

Here is code in java.

import java.util.*;

import java.lang.*;

import java.io.*;

class Solution

{

public static void main (String[] args) throws java.lang.Exception

{

try{

//declare and initialize string variables

String str1="password";

String str2="Pass_word";

//calling function to check valid password

System.out.println("output of password is: "+isValidPassword(str1));

System.out.println("output of Pass_word is: "+isValidPassword(str2));

}catch(Exception ex){

return;}

}

// functionn to check the valid password

public static int isValidPassword(String str)

{

// variables to count the lower,upper and non character

int i, lower_c = 0, upper_c = 0, non_c = 0;

char ch;

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

{

ch = str.charAt(i);

if(ch >= 'a' && ch <= 'z')

{//count of lower character

lower_c++;

}

else if(ch >= 'A' && ch <= 'Z')

{

//count of upper character

upper_c++;

}

else

{

//count of non character

non_c++;

}

}

if(lower_c > 0 && upper_c > 0 && non_c > 0) {

return 1;

} else {

return 0;

}

}

}

Step-by-step explanation:

Declare and initialize two string variables.In the isValidPassword() function, pass the string parameter and then count the lower, upper and non character in the string.If string contains call the three characters then function will return 1 otherwise it will return 0 .

Output:

output of password is: 0

output of Pass_word is: 1

User Kskaradzinski
by
5.2k points