-2

I'm supposed to check requirements of a string, and if all requirements are met, it should return true if not, then false.

The requirements are:

  • It must be 8 characters long
  • It must have one BIG letter
  • It must contain characters from 0 to 9

This is my:

public bool Check(string s)
{
    string buchstaben = "QWERTZUIOPÜASDFGHJKLÖÄYXCVBNM";
    string ziffern = "0123456789";
    foreach (char _c in s)
    {
        if (ziffern.Contains(_c) && buchstaben.Contains(_c) && s.Length == 8)
        {
            return true;
        }
    }
    return false;
}

It returns somehow always false.

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Kil Gore
  • 80
  • 8
  • You might want to look at https://stackoverflow.com/questions/19605150/regex-for-password-must-contain-at-least-eight-characters-at-least-one-number-a?rq=1, even if it is javascript and not c#. The solution is the same/similar. – Progman Oct 06 '19 at 17:04
  • @Progman I've seen it , but I want it without Regex – Kil Gore Oct 06 '19 at 17:05
  • 1
    `return s.Length == 8 && s.Count(Char.IsUpper) == 1 && s.Any(Char.IsDigit);` – dymanoid Oct 06 '19 at 17:09
  • @dymanoid Thank you very much, may I ask you what does s.Count do I mean how I know it counts the characters but how = – Kil Gore Oct 06 '19 at 17:23
  • @KilGore, `string` implements `IEnumerable`, and `s.Count` is a call to an extension method of that enumerable using the `Char.IsUpper` as method group that will be automatically converted to a delegate invocation. – dymanoid Oct 06 '19 at 17:27

6 Answers6

1

You can do this using Linq Any() and methods IsUpper and IsDigit. I believe the code below meets your requirements.

public bool Check(string s)
{   
    bool hasOneUpper = s.Count(c => char.IsUpper(c)) == 1;
    bool hasDigits = s.Any(c => char.IsDigit(c));        

    return !string.IsNullOrEmpty(s) && s.Length == 8 && hasOneUpper && hasDigits;
}
haldo
  • 14,512
  • 5
  • 46
  • 52
1
using System.Linq;  
public bool Check(string s)
{
    if ( s == null ) return false;
    if ( s.Length != 8 ) return false;
    if ( !s.Any(ch => char.IsDigit(ch))) return false;
    if ( s.Count(ch => char.IsUpper(ch)) != 1) return false;
    return true;
}

You still need to check if input isn't null

Here's an online Demo

AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Melchia
  • 22,578
  • 22
  • 103
  • 117
0
public bool Check(string s)
{
    bool retVal = false;
    char ch;
    totalcntupper = 0;
    for (char cnt = 0; cnt < s.length;cnt++)
    {
        ch = chars[cnt];
        if (char.IsUpper(ch))
        {              
            totalcntupper++;
        }
    }
    if (s.Length == 8 && totalcntupper == 1 && s.Any(char.IsDigit))
    {
        retVal = true;
    }
    return retVal;
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Josef
  • 2,648
  • 5
  • 37
  • 73
0

Here is a solution.

using System.Linq;

namespace ConsoleApp
{

  static partial class Program
  {
    static void Main(string[] args)
    {
      Test();
      Console.WriteLine();
      Console.WriteLine("End.");
      Console.ReadKey();
    }
    static void Test()
    {
      Console.WriteLine(Check("aaaaaaaa"));
      Console.WriteLine(Check("aaaaaAaa"));
      Console.WriteLine(Check("aa2aaaaa"));
      Console.WriteLine(Check("aa2aaAaa"));
      Console.WriteLine(Check("aa2aaÄa8"));
      Console.WriteLine(Check("Äa2aaAaa"));
      Console.WriteLine(Check("Äa2aaAa8"));
    }
    static public bool Check(string str)
    {
      return str != null
          && str.Length == 8
          && str.Any(c => char.IsDigit(c))
          && str.Count(c => char.IsUpper(c)) == 1;
    }
  }

}

The order of condition testing is for performance.

Output:

False
False
False
True
True
False
False
0

Your code always returns false because it requires for a character to be a letter and a digit at the same time:

ziffern.Contains(_c) && buchstaben.Contains(_c)

It is impossible so the execution never goes into the if body.

tsul
  • 1,401
  • 1
  • 18
  • 22
-1

public bool Check(String s) => s.Length >= 8 && s.Any(c => char.IsUpper(c)) && s.Any(c => char.IsDigit(c)) ;

The code above is a method assigned with boolean expression. You need to include linq in your using declaration for the String.Any() to work.

using System.Linq;

Ross Brigoli
  • 676
  • 1
  • 12
  • 22