Removed: Rule 4.
Dive in and make an attempt. Maybe first try to make a random password with characters regardless of uppercase and lowercase.
Then, while not elegant, maybe try using all the characters (both upper and lower case) randomly. Then do a check to see if the password happens to contain at least 1 lowercase and 1 uppercase character. If it doesn’t, then generate a new password and check again. Keep doing it until you get a valid password.
Kind of clunky, but assuming you have all the letters and numbers, it usually won’t take more than one or two generations.
Learn to google things. For this, you can google something along the lines of “C# check if string has uppercase letter.” I just did that and this was one of the first things that’s popped up and may help you: https://stackoverflow.com/questions/20032450/detect-if-a-string-contains-uppercase-characters.
Start by looking up the documentation for System.Char
.
Since this is homework, I’ll just give a hint, you may use the Contains method of the string that represents the password: https://docs.microsoft.com/en-us/dotnet/api/system.string.contains?view=net-5.0
Good luck!
Well there are some harsh answers here and i dont know why. After all everybody has to start somewhere. But the answers arent that incorrect google could help you there. But before you start google everything you should start to analyse your task. Split it in the little pieces and then start google with the little things. Like this:
How to get user Input in c#
How to use a string variable
How to check if string contains Uppercase
How to check if string contains Lowercase 5 How to return a result to the user
If you need more help to get started you may dm me and i will try my best to help you 😉
RegEx maybe? This website has a nice interface to write and test RegEx patterns along with a cheatsheet. Its “community patterns” tab for sure has some password RegExes
This is called validating user input. There are fancy ways to do this, but at your current level of expertise, I would suggest creating a function that takes the string input the user entered and goes character by character in a loop. The pseudo would be:
Func (input) { Bool hasLower = false; Bool hasUpper = false;
Foreach (char c in input) { If c is upper then hasUpper = true; If c is lower then hasLower = true; } Return (hasUpper && hasLower); }
There are multiple ways to optimize this but I will leave that for you.
Do I do this in the main method? Or do I have to create a new method?
Regex
Google: look-ahead regex
cheat sheet: overapi.com/regex
Try it at: regex101.com
If that is too much for you, go to your boss and tell them that they need to find somebody else to do the project. This is just about the easiest thing to implement.
Dude I started coding like not even a month ago… it’s homework so I can get in to a coding bootcamp
Bit harsh…
You’re going to want to loop over each character on the string. You should know how to loop. There are many ways to do that. You can use a foreach
, where you will automatically get each Char, or use for
and a counter variable and access each char with var currchar = somestring[counter]
.
That’s because the chars in a string can be accessed using the index operator []
.
You know what a string is and a char, and how strings are made up of chars, and what an array is and what an index
A char can be compared like a number, with greater than, less than symbols.
If you’re not using extended chars, just a-z, you can then check if a char is uppercase by
That’s basically testing if the char is in the range of uppercase values A to Z. Read up on ASCII if you haven’t yet. Basically characters are represented as numbers, and the uppercase characters A-Z are mapped to 65-90.
You can compare char variables to char literals (a literal means the text representation you type in your code, notice the single quotes!) because C# understands them as Chars, and can compare Chars with other Chars using >=
, <=
, ==
, !=
, <
, >
.
Similar thing for lowercase. You basically set a “flag” when you meet some condition. If the condition is met again, you just set it to true again. Its called a flag gor historic reasons.
Now that you’ve “flagged” your string as having uppercase AND having lowercase (hint hint) you react appropriately if the result of that is false.
Basically it’s
As you require more complex rules, you change how you test a char for some condition.
As always in programming there are many ways to go about something. Each method has it’s own pros and cons.
For example, you could have checked if your currchar can be found in a string that contains all the chars you want to test against for a specific condition. In may cases this would be the only way to test against a set of chars that don’t have a continuous range, like symbols !@#$%^&*
.
Now there is the String.Contains
method. This extension method loops through the string to do the comparision. It could be used in your this instance, but then you would need to loop over each character you want to test, then do String.Contains
, which means you will be looping over the string several times.
As you see, there are several methods to test, and not all of them the best way. Understand how code works (e.g. what Contains
does) and when and how to use it best.
Removed: Rule 5.
C# devs
null reference exceptions