Trying to make a program that checks if a password contains uppercase letter and a number in c

Related searches

I am trying to make a program that checks whether the password created contains an uppercase letter and a number. Both the for loops execute fine if only one of them is written without the other (one loop works fine if I comment out the other loop), but if I type them both together only the second for loop (that's associated with checking if there is a number or not) works and the other doesn't work.

So with the written code below, if I type a password that does not contain an uppercase letter nor a number I get the "Please make sure that your password contains a number and try again." message only.

Note :

  • I tried using the assigning the integer i to zero after the first loop and using it in the second loop instead of x but I get the same result.

  • I am using code::blocks compiler.

Could anyone point out where did I go wrong?

Sorry if the question isn't clear enough this is my first question here in stackoverflow, and thanks in advance.

The code :

#include <stdio.h>
#include <ctype.h>

int main()

{

    char password[25];

    int i;

    int x;

    printf("Create a strong password (must contain an uppercase letter and a number, and must be at least 8 characters) :\n");
    scanf(" %s", password);



    for (i = 0; i <= 25; i++)
    {

        if ( isupper(password[i]) == 1 )
        {
            break;
        }

        if (i == 25)
        {
            printf("\nPlease make sure that your password contains an uppercase letter and try again.\n");
        }


    }


      for (x = 0; x <= 25; x++)
    {

        if (isdigit(password[x]) == 1)
        {
            break;
        }

        if ( x == 25 )
        {
             printf("\nPlease make sure that your password contains a number and try again.\n");
        }

    }

    return 0;
}

There are a number of issues in your code, which I shall attempt to address here.

First, as mentioned in the comments, your two for loops will (most likely) go "out of bounds" as array indexes in C start at zero and end at "n - 1" (where "n" is the size of the array).

Second, the isupper() and isdigit() functions do not (necessarily) return "1" for a 'positive result' - the specification states only that they return non-zero. So, we can just test the return value as a 'Boolean' value (zero will equate to 'false' and any non-zero will equate to 'true').

Lastly, rather than run your for loops for the entire password buffer, you need only run until the terminating nul character is found - at which point you will know that a required uppercase letter or digit is not including in the string.

Here's a 'working' version of your code, with comments added where I've made changes:

int main()
{
    char password[25];
    int i;
    int x;
    printf("Create a strong password (must contain an uppercase letter and a number, and must be at least 8 characters) :\n");
    scanf(" %s", password);
    for (i = 0; i < 25; i++) { // password[24] is the last possible element - NOT password[25].
        if (isupper(password[i])) { // "isupper()" will return NON-ZERO (but not necessarily 1!)
            break;
        }
        else if (password[i] == '\0') { // we've reached the end of the string and not found an uppercase...
            printf("\nPlease make sure that your password contains an uppercase letter and try again.\n");
            break;
        }
    }
    for (x = 0; x < 25; x++) { // as before, use "x < 25" rather than "x <= 25"
        if (isdigit(password[x])) { // "isdigit()" will return NON-ZERO (but not necessarily 1!)
            break;
        }
        else if (password[x] == '\0') { // we've reached the end of the string and not found a digit...
            printf("\nPlease make sure that your password contains a number and try again.\n");
            break;
        }
    }
    return 0;
}

Checking that a password contains a '$', a digit, and an uppercase , So the point of this program is to check a password, right? Should have a header for those function prototypes, and perhaps a separate source file for the The C standard knows how frequently this is used, and lets us omit it. a $ character. Please, when it comes to encryption, don't try and roll your own, or just find� Contribute your code and comments through Disqus. Previous: Write a C program to check if two numbers in a pair is in ascending order or descending order. Next: Write a C program to read the coordinate(x, y) (in Cartesian system) and find the quadrant to which it belongs (Quadrant -I, Quadrant -II, Quadrant -III, Quadrant -IV).

Here you have a simple function:

int isCapitalAndDigit(const char *pwd, int minsize)
{
    int isCapitalLetter = 0, isDigit = 0;
    size_t len = 0;

    while(*pwd && (!(isDigit && isCapitalLetter) || len < minsize) )
    {
        if(isdigit(*pwd)) isDigit = 1;
          else if(isalpha(*pwd) && !islower(*pwd)) isCapitalLetter = 1;
        pwd++;
        len++;
    }
    return isDigit && isCapitalLetter && (len >= minsize);
}

https://godbolt.org/z/53XrhW

Program to check the validity of a Password, Password checker program basically checks if the password is valid or not based on password policies Password should contain at least one uppercase letter( A-Z). Recommended: Please try your approach on {IDE} first, before moving on to the solution. To convert int to string char c = ( char )i;. A password is said to be strong if it satisfies the following criteria: It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+ Its length is at least 8. It contains at least one digit. Given a string, find

Here's c++ code that uses regular expressions (with a bit of work this should be do-able in C ?)

#include <iostream>
#include <iterator>
#include <string>
#include <regex>

int main()
{
    std::string s = "tkfgfff3gf";


    std::regex word_regex("(\[[:upper:]])");    


    auto words_begin = 
        std::sregex_iterator(s.begin(), s.end(), word_regex);
    auto words_end = std::sregex_iterator();

    int count =std::distance(words_begin, words_end);

    if(count == 0)
    {
        std::cout<<"Please make sure that your password contains an uppercase letter and try again";
    }

    std::regex number_regex("(\[0-9])");

    auto number_begin = 
        std::sregex_iterator(s.begin(), s.end(), number_regex);
    auto number_end = std::sregex_iterator();

    count =std::distance(number_begin, number_end);

    if(count == 0)
    {
        std::cout<<"Please make sure that your password contains a number and try again";
    }

    return 0;


} 

Program to check Strength of Password, It contains at least one uppercase English character. Recommended: Please try your approach on {IDE} first, before moving on to the solution. C++ program to check if a given password is Checking lower alphabet in string Get hold of all the important DSA concepts with the DSA Self Paced Course� I am trying to make a password verifier. When i try to check for an uppercase, lower case or number if does read it. I Think it reads the first letter. How can i make it read the whole array? thanks in advance.

C# Password Check Exercise - C#, Can you please email the full solution code for the C# Password Checker Why would you need the full solution mailed if you can get it with solution button? exercise url: https://www.codecademy.com/courses/learn-c-sharp/ WriteLine(“ Password does not contain an uppercase letter, INSECURE!”); I am writing a program that is supposed to take a string (the password entered by user) and test it to make sure it meets the following requirements: must contain at least one uppercase letter and one lowercase letter must start with a letter minimum of eight characters no blanks must contain at least two digits

Input Validations in C# at C Sharp for Beginners Course Codeasy.Net, Discover how to perform validation in C Sharp applications to prevent a user from "Ok, rule number one. The user can input wrong data however, and whenever, she wants." In this next code, we'll add a check to see if the user actually entered a string, and if "Password should contain at least one uppercase letter." Surely you know what an uppercase letter is: A, B, C, D etc Lower case: a, b, c, d etc Digits = numbers: 1,2,3,4,5 etc Symbols: ?, !, *, & etc Take ‘Elizabeth’.

i would like to know if it's possible check if one letter of a string is capitalized. Other way to see it, if all letters in the string are uppercase or lowercase. Example: string a = "aaaaAaa";

Comments
  • One major problem is that the loop for (i = 0; i <= 25; i++) will go out of bounds. It will also disregard the end of the string (if it's less than 24 characters).
  • isupper does not return specifically 1, but a non-zero value to be true. So if(isupper(password[i]) == 1) should be if(isupper(password[i]))
  • if (isupper((unsigned char)password[i])) /* ... */; to allow for password to have, for example, the 'π' character
  • You should also stop reading after encountering the \0 (null) character.
  • isupper() returns Non-zero integer ( x > 0 ) when argument is an uppercase alphabet, returns 0 when argument is not an uppercase alphabet. Alternate way is to compare with the ASCII values. if(password[i] >= 65 && password[i] <= 90).
  • And maybe scanf(" %24s", password);
  • for (i = 0; password[i]; i++) is probably the better condition anyway (you just have to make sure, that the conversion in scanf() succeeded previously. This would however require some more changes...
  • Your code will treat any of these: +=&*£$ (and many more) as 'Capital Letters'!
  • Yes, but now you have isalpha(*pwd) && !islower(*pwd) which is a very bizarre way of re-encoding isupper(*pwd) - is it not?
  • Thanks for the help but I am a still learning C and I didn't learn about creating my own functions yet. And that's my bad I forgot to mention it while typing the question. I will make sure to check out your answer once I learn about creating functions. Thanks again!
  • Thanks for the help, but I am still learning C and didn't learn anything about C++ yet. And that's my bad I forgot to mention in it in the question.
  • Yes i wasn't expecting you to be able to use this, this is more to show you how much easier using reg expressions are.
  • Thanks for the help anyways mate. Much appreciated!!