FAQ: Understanding Logic in C# - Review

This community-built FAQ covers the “Review” exercise from the lesson “Understanding Logic in C#”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn C#

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,

I noticed there weren’t any solutions or discussions regarding the two exercises of lesson 5 so i will post my own for the first problem at the end. I’m posting it in case anyone gets stuck. I do not know if it is the best solution or if there are easier more efficient ways but this one is straight forward and contains concepts covered in the previous lessons. Kind of. With further comments here it is, explanations in the code comments:

using System;
using System.Text;

namespace FifthLesson
{
    class PasswordCheck
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please input a password:");
            string passStr = Console.ReadLine();
            string message = "";
            bool upFlag = false;
            bool symbolFlag = false;
            byte[] asciiBytes = Encoding.ASCII.GetBytes(passStr);

            //First loop through each value of the array and see if is in the range of ASCII values of upper case letters
            //If we find just one upper case we set the upper case flag to true and break from the for and continue to next step. 
           for (int i = 0; i <= asciiBytes.Length-1; i++)
            {
                if (asciiBytes[i]>=65 && asciiBytes[i]<=90) 
                {
                    message = "String contains upper case.";
                    upFlag = true;
                    break;
                }
            }
           //Second we loop again through it searching for symbols, again within the designated ASCII table range of symbols
           //If we find just one symbol, then we set the flag to true break again and continue. 
            for (int i = 0; i <= asciiBytes.Length - 1; i++)
            {
                if ((asciiBytes[i]>=33 && asciiBytes[i]<=64) || (asciiBytes[i] >= 91 && asciiBytes[i] <= 96) || (asciiBytes[i] >= 123 && asciiBytes[i] <= 126))
                 {
                        message = message + "Unfortunately it also has symbols";
                        symbolFlag = true;
                        break;
                 }
            }

            //Based on the flags we set previously write the corresponding message to the console
            if (upFlag == true && symbolFlag == false)
            {
                Console.WriteLine("Password has upper case and not symbols.");
            }
            else if (upFlag == true && symbolFlag == true)
            {
                Console.WriteLine("Password has upper case and also symbols.");
            }
            else
            {
                Console.WriteLine("Password has no upper case and no symbols.");
            }

            Console.ReadLine();
        }
    }
}

Cheers,
Vladisan

7 Likes

Nope, never would have gotten that one.

1 Like

I’ve done it this way:

using System;
using System.Linq;

namespace CodeCademyLogic
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get a password to check
            Console.WriteLine("Please input a Password: ");
            string userPassword = Console.ReadLine();

            //Check the string
            bool upperCheck = userPassword.Any(char.IsUpper);
            bool symbolCheck = userPassword.Any(p => !char.IsLetterOrDigit(p));

            //Write results to console
            Console.WriteLine($"Symbols are present: {symbolCheck} \n Uppercase letters are present: {upperCheck}");    
        }
    }
}


Some of this is based on what I’ve learned so far, but I made very good use of Google as well!
Bools can only be true or false, so I’ve not bothered stating as much in the code. Instead, if the bool meets IsUpper / Is not IsLetterOrDigit, it returns True. If not, then it returns False.

The p=> and ( p ) is an anonymous method and checks each char of the string.
!’ is the C# operator for ‘NOT’,

This works in Visual Studio but not in CodeCademy.

6 Likes

Hi,
Using the information that has been learned so far, this was my approach to solving this problem.

Console.Write("Please input a password: ");
string userPassword = Console.ReadLine();

//Create a new variable that makes the user’s input all lowercase
string lowerCasePassword = userPassword.ToLower();

//Compare this to what the user wrote and see if they are equal.
//If not, then there is at least 1 uppercase letter
bool upperCheck = userPassword != lowerCasePassword;

//To check for symbols, use IndexOf and search for common symbols such as !,>,<,@,#,$ etc
//If the index value is greater than 0 then there is a symbol in the string
bool symbolCheck1 = userPassword.IndexOf("!",0) > 0;
bool symbolCheck2 = userPassword.IndexOf("@",0) > 0;
bool symbolCheck3 = userPassword.IndexOf("#",0) > 0;
bool symbolCheck4 = userPassword.IndexOf("%",0) > 0;

bool symbolCheck = symbolCheck1 || symbolCheck2 || symbolCheck3 || symbolCheck4;

bool finalCheck = symbolCheck || upperCheck;

if (finalCheck == true)
Console.WriteLine(“there is at least one uppercase letter or symbol in the password”);
else if (finalCheck == false)
Console.WriteLine(“There are no uppercase letters nor symbols”);
else
Console.WriteLine(“error!”);

5 Likes

This is ridiculous, we haven’t learnt anything about character parsing in this lesson, and yet we get an exercise (without any solution provided) that includes parsing a string! If I have to google how to do the exercise, what’s the point of doing this course? I can just do it on google…

7 Likes

Nice. Straightforward and easy to understand… Really cool approach.

agree. please provide a solution at the very least. i know how if conditions and for loops are implemented (im doing the course to refresh on my c#), but i found this exercise to be rather odd.

1 Like

I did mine this way using the help of Googling some basic concepts like the char.IsUpper/char.IsLetterOrDigit methods. I feel like this challenge is unusually tough given that we haven’t even learnt if statements yet, but it was quite fun trying to solve this problem and doing research on it. Couldn’t understand some of the replies for this review like the System.Linq, anonymous methods. I really like ranger026’s one and thought about checking each symbol individually but felt that maybe some symbols would be left out, the uppercase check was really clever though using simple code.

using System;

namespace Review
{
  class Program
  {
      static void Main(string[] args)
      {
      Console.WriteLine("Please input a password: ");
      string password = Console.ReadLine();
      int upcaseCount= 0; 
      int symbolCount= 0; 

       //For loop to cycle in all the characters in the password string since the Char methods only work on characters not strings
      for (int i = 0; i < password.Length; i++) 
      { 
      if (char.IsUpper(password[i])) upcaseCount++; 
      if (char.IsLetterOrDigit(password[i])) symbolCount++; 
      }
      if (symbolCount > 0) Console.WriteLine("Password has symbols!");
      else Console.WriteLine("Password has no symbols!");       
      if (upcaseCount  != 0) Console.WriteLine("Password has uppercase!");
      else Console.WriteLine("Password has no uppercase!");
      }
  }
}

Note:
symbolCount > 0, upcaseCount != 0, I did them differently just to have fun using different comparison operators, but you can use the same for both.

I had to learn about ‘for’ loops, ‘if-else’ statements and the char methods on top of the stuff we learnt thus far. I found that using only knowledge from the past exercises would result in a tedious long code to catch all the symbols, but the closest application of it would probably be ranger026! Not sure how we can do it without ‘if’ statements though

2 Likes

After reading through these posts I tried approaching this as you have, recreating it in my own head rather than straight copying. But I quickly hit a problem where I kept getting an error.

After some time trying to figure it out I thought I’d plough on and hope it would get resolved with the addition of the rest of the program…or something. But I couldn’t check anything because this error meant I couldn’t see what else may or may not be working. So I ended up copy/pasting the second half of you code (having to alter the quotation marks, as they appear to be the wrong ones in the code) and thought I’d give it a go to see if having the full thing would solve it… nope.

I then tried copy/pasting ALL of your code. Ran it and…same error I got at the start. Apparently it’s seeing something wrong with the line “string lowerCasePassword = userPassword.ToLower();”. Here the full error text:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Review.Program.Main(String args) in /home/ccuser/workspace/csharp-understanding-logic-review-understanding-logic-csharp/Program.cs:line 22

(“line 22” is that line I quoted above).

Has anyone else had any problems getting a line of code to run?
My Console.Readline(); is being ignored and I do not know why.

using System;

namespace Review

{

class Program

{

static void Main(string[] args)

{

  /* use this space to write your own short program! 

  Here's what you learned:

  BOOL TYPE: bool variableName

  COMPARISON OPERATORS: ==, <, >, <=, >=

  LOGICAL OPERATORS: &&, ||, !

  Good luck! */

  string test = "test";

  Console.ReadLine();

  Console.WriteLine(test);

  Console.ReadLine();

  Console.WriteLine(test);

        

}

}

}

The result is

test
test

without any prompt for input. I’ve switched over to Visual Studio for now but cannot understand why this is happening.

1 Like

hey @web1189312547, One of the authors of Learn C# here.

You’re not alone in this confusion! I’ve seen this come up a few times and I’m thinking about ways to avoid it in the future.

There are two “setups” in the Learn C# course: an output terminal and an interactive terminal.

  • This Review exercise uses an output terminal (you can’t type into it). ReadLine() is essentially skipped when using an output terminal.
  • Some other exercises uses an interactive terminal - you must type dotnet run to run code. ReadLine() DOES work in interactive terminals.

Try this code in an exercise with an interactive terminal: it should work as expected!

1 Like