Ask Question
23 December, 01:51

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"

+5
Answers (1)
  1. 23 December, 05:17
    0
    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
    {

    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;

    }

    }

    }

    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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a function named "isValidPassword" that takes in a string and returns 1 if it stores a valid password and 0 otherwise, A valid ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers