Why it's doesn't work? Password Rules. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why it's doesn't work? Password Rules.

I can't solve password rules. Dont recommend to check the solution!!! I cant complete 5 Test. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string password = Console.ReadLine(); char[] nAS = { '!', '#', '

#x27;, '%', '&', '(',')', '*', ',', '+', '-' }; //введите код сюда for(int x = 0; x < nAS.Length; x++){ if(password.Contains(nAS[x])) Console.WriteLine("Invalid"); } //Btw ik there must be break but where do i put it? } } } }

4th Jan 2023, 5:48 PM
Mihail Jimi
Mihail Jimi - avatar
22 Answers
+ 4
for(int x = 0; x < nAS.Length; x++) { if(password.Contains(nAS[x])) { Console.WriteLine("Invalid"); break; } } Hoping this is what you tried... You're welcome...
4th Jan 2023, 6:42 PM
Jayakrishna 🇮🇳
+ 3
Mihail Jimi can you add full task description, for what the code supposed to do ? Hassanah password.Contains(password[x]) is always comes true. It's not correct. Recheck it. @Mihail is right way here.
4th Jan 2023, 6:09 PM
Jayakrishna 🇮🇳
+ 1
What are the rules ? Your code outputs "Invalid"(matching times sequencly) , if input contains '!' , Otherwise nothing.. Revise about if-else again once. What is supposed to print if password is valid..? You are not put anything...
4th Jan 2023, 5:58 PM
Jayakrishna 🇮🇳
+ 1
If password is valid it dont need to type anything
4th Jan 2023, 6:01 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
There must be break somewhere (i've changed the description). But still where do i put it to avoid outputing nothing.In example if user's password contains "*" my code will output nothing because if i put "break;" at the end the "for" will work only 1 time.
4th Jan 2023, 6:04 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
(password.Contain(password[x]) That's doesn't work
4th Jan 2023, 6:13 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
Jayakrishna🇮🇳 The given program takes the password as input. Task Write a program to output "Invalid", if it contains any disallowed symbols. If the password requirement is satisfied, program shouldn't output anything. Sample Input yl1893!dm$ Sample Output Invalid Hint The message should be output only once regardless of how many disallowed symbols the password contains.
4th Jan 2023, 6:16 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
Jayakrishna🇮🇳 I tried. The loop will work only 1 time if i put break there. So it will check if there "!" then breaks the loop the loop wont check other characters.
4th Jan 2023, 6:19 PM
Mihail Jimi
Mihail Jimi - avatar
+ 1
It should be inside if block. so it works as needed. You are missing curly braces may be.. Add curly braces..
4th Jan 2023, 6:22 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 Doesn't help...
4th Jan 2023, 6:32 PM
Mihail Jimi
Mihail Jimi - avatar
0
Hello there, I'm not sure but i think it's because you missed out } somewhere Try: string password = Console.ReadLine(); char[] nAS = { '!', '#', '
#x27;, '%', '&', '(',')', '*', ',', '+', '-' }; //введите код сюда for(int x = 0; x < password.Length; x++) { if(password.Contains(password[x])) { Console.WriteLine("Invalid"); } else { break; } }
4th Jan 2023, 5:58 PM
Hassanah
Hassanah - avatar
0
Oh yes I think i forgot to point out that in your for loop you put nAS[x] instead of password[x] and the break statement should be in the if statement.
4th Jan 2023, 6:00 PM
Hassanah
Hassanah - avatar
0
Also ignore my code from previously I messed it up a bit. Try this: string password = Console.ReadLine(); char[] nAS = { '!', '#', '
#x27;, '%', '&', '(',')', '*', ',', '+', '-' }; //введите код сюда for(int x = 0; x < nAS.Length; x++) { if(password.Contains(password[x])) { Console.WriteLine("Invalid"); } else { break; } }
4th Jan 2023, 6:02 PM
Hassanah
Hassanah - avatar
0
You just forgot some curly brackets and you should have put if (password.Contain(password[x]) Hope that helps
4th Jan 2023, 6:03 PM
Hassanah
Hassanah - avatar
0
Oh yes in the if statement
4th Jan 2023, 6:04 PM
Hassanah
Hassanah - avatar
0
To avoid it repeatedly outputting invalid
4th Jan 2023, 6:04 PM
Hassanah
Hassanah - avatar
0
if(password.Contains(password[x])) { Console.WriteLine("Invalid"); break; }
4th Jan 2023, 6:06 PM
Hassanah
Hassanah - avatar
0
using System; namespace SoloLearn { class Program { static void Main(string[] args) { string password = Console.ReadLine(); char[] specialCharacters = { '!', '#', '
#x27;, '%', '&', '(',')', '*', ',', '+', '-' }; bool isValid = true; foreach (char c in password) { if (specialCharacters.Contains(c)) { isValid = false; break; } } if (isValid) { Console.WriteLine("Valid"); } else { Console.WriteLine("Invalid"); } } } } In this version, I have used a foreach loop to iterate through each character in the password. I have also introduced a bool variable isValid to track whether the password is valid or not. The loop checks if the current character is contained in the specialCharacters array. If it is, isValid is set to
5th Jan 2023, 9:34 PM
Aditya Dixit
0
using static System.Console; //using System.Collections.Generic; using System.Linq; //using System.Text; //using System.Threading; //using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { string password = ReadLine(); //for what you use char[]??? // char[] nAS = { '!', '#', '
#x27;, '%', '&', '(',')', '*', ',', '+', '-' }; string nAS = "!#$%&()* +-"; //введите код сюда /* for(int x = 0; x < nAS.Length; x++){ if(password.Contains(nAS[x])) you call console every time when there is match.. Console.WriteLine("Invalid"); } */ //use Linq for so simple code.. if(password.Count(nAS.Contains)>0) WriteLine("Invalid"); //Btw ik there must be break but where do i put it? /* or regex if(new Regex(@"[!#$%&()* +-]") .IsMatch(password)) WriteLine("Invalid"); */
6th Jan 2023, 9:45 AM
Smith Welder
Smith Welder - avatar
- 1
In that case: Mihail Jimi Remove else part. and add break just after output statement. So it prints "invalid", then breaks loop...
4th Jan 2023, 6:17 PM
Jayakrishna 🇮🇳