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

2 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.

1 Like

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!”);

1 Like

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…

3 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.