FAQ: Working with Text - Building Strings

This community-built FAQ covers the “Building Strings” exercise from the lesson “Working with Text”.

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

Learn C#

FAQs on the exercise Building Strings

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!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

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!

1 Like

can anyone help me with this prob?

Hello, @ajax5155666195.

Error messages are very useful. Your error message tells you exactly what the problem is, and where to find it.

Program.cs(15,138): Look at this first part of the message. It is telling you that the problem occurred on line 15 of the Program.cs file. The file in your code editor is the Program.cs file. It even tells you where in the line the compiler encountered the problem: the 138th character.
Next part:
error C51002: ; expected It looks fairly obvious that the compiler was expecting to find a ;. It did not, so the build failed.
If you look at line 15 of your code, knowing that the compiler was expecting a ;, but didn’t find one …

3 Likes

For exercise 3 of the lesson, Codecademy asks of you to perform the following option: image
(There are two string variables, firstSentence and firstSpeech that the website wants you to print)

I did exactly that using the following line of code:

  Console.WriteLine(firstSentence + "\n\n" + firstSpeech);

However, Codecademy marked that as incorrect and then requested that I write it this way instead:

  Console.WriteLine(firstSentence);
  Console.WriteLine("\n");
  Console.WriteLine(firstSpeech);

Why is my answer incorrect if it does the same thing, but in less lines?

4 Likes

Hello, @makingpoutine.

Your code actually prints 2 newlines between them instead of the 1 requested. It’s still possible, however, that the SCT for the exercise is expecting 3 Console.WriteLine() statements. You might re-try your code with a single newline, and see if it’s accepted.

Edit:
Just looked at the exercise. To get the expected output you actually need "\n\n\n", but the SCT is expecting 3 Console.WriteLine() statements in order to pass the step.

1 Like

I don’t understand the last step.

using System;

namespace PrideAndPrejudice
{
class Program
{
static void Main(string args)
{
// First string variable
string firstSentence = “It is a truth universally acknowledged, that a single man in possession of a good fortune, must be in want of a wife.”;
// Second string variable
string firstSpeech = "\My dear Mr. Bennet, said his lady to him one day, “have you heard that Netherfield Park is let at last?”;

  // Print variable and newline
    Console.WriteLine("firstSentence\n");
}

}
}

okay Thankyou This is very useful!!!

1 Like

Thank you @midlindner

I have reached at this exercise just now and faced a similar issue.

Your explanation was so well written that when I came back to the exercise I knew exactly where to correct my code.

Thanks once again!

1 Like
      // Print variable and newline
      Console.WriteLine($"{firstSentence}\n{firstSpeech}");

Isn’t accepted by the lesson even though it does what is asked for (or am I missing something?)

1 Like

This lesson is different from a lot of others in that the solution emphasizes how you coded as opposed to whether your code does what is asked. Take a look at the previous posts on this subject.

This is another example of poor instructions and lesson coding from Codecademy.

The task is simply " Using Console.WriteLine() , print each variable to the console. In between them, print a newline to the console."

Nowhere does it say it has to be done with three lines of Console.WriteLine(). I used the same technique jotoer used, which is exactly recommended in the Microsoft documentation as the best way to bring strings together. Not sure why I shouldn’t just use the tutorial on Microsoft’s website, since it seems much better. Definitely turned off from becoming a PRO member if this is the lesson quality I can expect.

5 Likes

I’m very glad you answered this question - the parenthetical numbers were never explained by the material, and that has been frustrating me. Thank you for taking a few minutes to explain it! That made a difference to me.

1 Like

To anyone who may still be struggling with this, check the color of the ; at the end of your string. If the ; is colored, it is considered part of the string. If ; is white, it is seen at the end of the statement.

Screen Shot 2021-08-03 at 1.12.36 PM

Got a little frustrated that

Console.WriteLine(firstSentence + “\n” + firstSpeech);

did not work.

It gave the desired result in the console, but would not give me the checkmark.

The actual answer is

Console.WriteLine(firstSentence);
Console.WriteLine(“\n”);
Console.WriteLine(firstSpeech);

CodeCademy wants the user to write three WriteLine functions instead.
What makes me scratch my head is that if I’m writing a third WriteLine function, why would I even bother putting the new line in there? Seems like I’m being punished for solving the problem as efficiently as possible.

I guess it makes sense though, codecademy at this point in the lesson did not go over concatenation, or at least I don’t think it did.

3 Likes

I did literally what the instructions say, and the code runs, but the lesson won’t let me pass:

Console.WriteLine(firstSentence + "\n" + firstSpeech);

It says “use Console.WriteLine()”… I did that, and the output is correct (though I think it should use two \n not one, but whatever) Could someone please fix this lesson?

Since the exercises are checked by an automated system (instead of a human checking each submitted solution), so the exercises are very particular that the output should meet the specifications in the instructions exactly. If there is extra/less punctuation, uppercase/lowercase different than specified or different spacing, then the system usually doesn’t accept the solution.

The instructions for Step 3 specify:

Using Console.WriteLine(), print each variable to the console. In between them, print a newline to the console.

The intended solution is:

Console.WriteLine(firstSentence);
Console.WriteLine("\n");
Console.WriteLine(firstSpeech);

which will produce the following output:

scrn1

Your solution is:

Console.WriteLine(firstSentence + "\n" + firstSpeech);

which produces the output:
scrn2

It may be annoying and nitpicky since it is only a slight difference. But to an automated system, that is enough of a difference.