+ 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? } } } }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...
+ 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.
+ 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...
+ 1
If password is valid it dont need to type anything
+ 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.
+ 1
(password.Contain(password[x])
That's doesn't work
+ 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.
+ 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.
+ 1
It should be inside if block. so it works as needed.
You are missing curly braces may be..
Add curly braces..
+ 1
Jayakrishna🇮🇳 Doesn't help...
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;
}
}
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.
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;
}
}
0
You just forgot some curly brackets and you should have put if (password.Contain(password[x])
Hope that helps
0
Oh yes in the if statement
0
To avoid it repeatedly outputting invalid
0
if(password.Contains(password[x]))
{
Console.WriteLine("Invalid");
break;
}
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
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");
*/
- 1
In that case: Mihail Jimi
Remove else part. and add break just after output statement. So it prints "invalid", then breaks loop...