C# for JavaScript Developers: Basic Exercises 03

Link to the exercises I’ll be working through from the Breaking IT Down Blog.

This is part three in a series I’m writing working through some basic exercises in C# as a JavaScript developer. The same disclaimer applies to this article that applies to the others. Though C# is built around Object Oriented Programming, I won’t be using OOP principles in the code examples in this article. I’m using these exercises to demonstrate the differences and similarities in syntax between JavaScript and C#, and I’ll be refactoring the code later to be more in line with OOP’s practices.

If you have never looked at C# code before, I think you’re going to be surprised at how similar it can be to JavaScript!

Exercise Three

The tasks in each exercise will be in bullet points, and each task will have its solution directly below it in a code block. For more

string message = "Please enter a passphrase."
string input = Console.ReadLine();
Console.WriteLine("Please enter any number greater than 9.");
string num = Console.ReadLine();
string password;

This is also syntax to declare a variable that works in JavaScript, though in JS it would be let, constor var instead of string . The important thing is that in C#, a variable can be declared without a value being stored inside it, just like JavaScript. The difference is that C# needs the type declaration of that variable as well as its name, and the type cannot change later. Read more about types in C#.

1)Remove any spaces from the input

Regex rgxSpace = new Regex("/ /gm");
password = rgxSpace.Replace(input, "");

See the second article in this series for more information on regular expressions in C#.

2) Replace the letter a with the number 2

Regex rgxA = new Regex("/a/gmi");
password = rgxSpace.Replace(input, "2");

3) Replace the letter o with the number 0

Regex rgxO = new Regex("/o/gmi");
password = rgxSpace.Replace(input, "0");

4) Replace the letter i with an exclamation mark

Regex rgxI = new Regex("/i/gmi");
password = rgxSpace.Replace(input, "!");

5) Add the last digit of the number to the beginning of the password

6)Add the first digit of the number to the end of the password

// this line fulfills both 5 and 6
password = num[0] + password + num[num.Length-1];
Console.WriteLine($"Original phrase: {passphrase} \nNumber: {num}\n New password: {password}");

Here we see that the newline character \n can be used in C# just like in JS, though strings in C# cannot be multiline unless broken up with concatenation like this:

string multiline = "this" + 
"string" +
"is" +

And that’s the third exercise on the worksheet! In the next article I’ll be refactoring the code from the last few exercises so it is more object oriented.

I hope this helps dispel any fears you may have of learning a new language. It was daunting for me from the outside, but once I actually started to look into the details of how it is written, C# got a whole lot more welcoming.

Github repo for this project.

I’m a web developer focusing on interactive projects using React and Nodejs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store