C# String Arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

24th Jan 2021, 9:30 AM
Nazar Sumyk
Nazar Sumyk - avatar
11 Answers
+ 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; } } ...
24th Jan 2021, 1:02 PM
Lothar
Lothar - avatar
+ 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
25th Jan 2021, 10:06 AM
Lothar
Lothar - avatar
+ 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
24th Jan 2021, 10:22 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 3
You have two loops, break just ends the inner loop. You get multiple output if password contains multiple disallowed symbols
24th Jan 2021, 9:42 AM
Benjamin Jürgens
Benjamin Jürgens - avatar
+ 2
so what need add or delete or change? help ._.
24th Jan 2021, 9:52 AM
Nazar Sumyk
Nazar Sumyk - avatar
+ 1
Last - closed test (5) wrong
24th Jan 2021, 9:31 AM
Nazar Sumyk
Nazar Sumyk - avatar
+ 1
Lothar , it’s cool but i don’t learn this method :) thank you! 😅
25th Jan 2021, 3:29 PM
Nazar Sumyk
Nazar Sumyk - avatar
+ 1
foreach(char k in notAllowedSymbols ) { if( password.Contains(k)){ Console.WriteLine("Invalid"); break; }
29th Aug 2022, 10:36 PM
Mohammed AbuJayyab
+ 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; } }
12th Jun 2023, 3:16 PM
Mohammed Safaf PT
Mohammed Safaf PT - avatar
0
Can someone please explain why it is failing the fifth test?
7th Mar 2023, 11:17 PM
Kevin Bucek
Kevin Bucek - avatar
- 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
13th Mar 2022, 7:09 PM
Zahra Tavakoli