+ 2
C# String Arrays
https://code.sololearn.com/cJcj2318Tgz5/?ref=app My code passed 4 out of 5 tests. What is wrong? Task: You are creating an authentication system. The password shouldn't contain any of these symbols: char[] notAllowedSymbols = { '!', '#', '
#x27;, '%', '&', '(', ')', '*', ',', '+', '-' }; Write a program to take the password as input and 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. Use the for/foreach loop to iterate through the array of disallowed symbols and check the condition, then use the break keyword to stop the iteration if the condition is evaluated as true.11 Antworten
+ 7
Nazar Sumyk , there is no need to have 2 loops. you can use one loop picking the elements and then use string method .Contains() to check for element. so it could look like this: (AND PLEASE: -->> use proper formatting of your source code. this helps reading and understanding the code)
...
for (int i = 0; i < notAllowedSymbols.Length; i++)
{
if (password.Contains(notAllowedSymbols[i]))
{
Console.WriteLine("Invalid");
break;
}
}
...
+ 5
Nazar Sumyk , i have done an other simple version. it is using the Intersect() extension method on Enumerable. It works on any IEnumerable<T> including arrays. to make this code run, password and sequence with "notAllowedChars" have to be the same data format. so the input of the password will be converted directly. in this case i used char[] array. but it does also work with 2 strings.
see the code in the file:
https://code.sololearn.com/ct77OTs6ztHQ/?ref=app
+ 4
There are many possible solutions to fix. Just some ideas:
- 1 loop and string.Contains()
- bool variable "invalid" (the C way)
- Function and return instead of break
+ 3
You have two loops, break just ends the inner loop. You get multiple output if password contains multiple disallowed symbols
+ 2
so what need add or delete or change? help ._.
+ 1
Last - closed test (5) wrong
+ 1
Lothar , it’s cool
but i don’t learn this method :)
thank you! 😅
+ 1
foreach(char k in notAllowedSymbols )
{
if( password.Contains(k)){
Console.WriteLine("Invalid");
break;
}
+ 1
string password = Console.ReadLine();
char[] notAllowedSymbols = { '!', '#', '#x27;, '%', '&', '(', ')', '*', ',', '+', '-' };
//your code goes here
foreach(char ch in notAllowedSymbols)
{
if(password.Contains(ch))
{
Console.WriteLine("Invalid");
break;
}
}
0
Can someone please explain why it is failing the fifth test?
- 2
Hello everyone I need you r help I don't know c# but my teacher wants sololern certificate c# I can't pass project please help