Start learning free

LESSON 66 OF 289 · STRINGS I

Top K Brands V1 in C#: Building Lines with String Concatenation

Practice Read the full explanation

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

        string product = "Laptop";
        string brand = "TechPro";
        int rating = 4;

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (productLine and brandLine), join them with a newline "\n" and print the result with Console.WriteLine(...).

C#'s + on a string and an int converts the integer to text for you.

productLine + "\n" + brandLine joins both lines into one string.

        string product = "Laptop";
        string brand = "TechPro";
        int rating = 4;

        string productLine = product;
        string brandLine = "1. " + brand + " (" + rating + ")";
        Console.WriteLine(productLine + "\n" + brandLine);

STEP 1 OF 3
    Ctrl + Enter to check

    C# · STRINGS I

    Top K Brands V1 in C#, explained

    Problem overview

    A shopper searches for a product, and the page shows the top-rated brand that sells it. In this first version there is just one product and one brand, held in three variables: product (a string), brand (a string) and rating (an int). The program prints two lines. The first is the product name. The second is a ranked entry in the form 1. Brand (rating).

    For a Laptop sold by TechPro with a rating of 4, the output is:

    Laptop
    1. TechPro (4)

    Each line is first stored in its own variable, productLine and brandLine. The program then joins them with a newline and prints the result with a single Console.WriteLine call.

    Approach: build each line as a string, then join

    The task is all about strings. The first line is the product name unchanged. The second line is made of five pieces glued together: the rank "1. ", the brand, the text " (", the rating and the closing ")". Reading the output left to right gives you the order of the pieces.

    The + operator joins strings. If one side is a string and the other is a number, C# converts the number to text and joins it, so rating can go straight into the expression with no conversion call. Spaces are part of the pieces: "1. " ends with a space, and " (" starts with one. That is where the spaces in 1. TechPro (4) come from.

    Keeping each line in its own variable makes the last step trivial and keeps the building of the lines separate from the printing.

    Step by step

    1. Create productLine as a string and set it to product. The first output line is just the product name.
    2. Create brandLine as a string: "1. " + brand + " (" + rating + ")". Join the pieces with + in the order you read them.
    3. Print both lines with one call. Join productLine, the newline "\n" and brandLine, and pass the result to Console.WriteLine. The escape sequence \n inside a string is a single newline character: it makes the text continue on a new line where it sits.

    The solution in C#

            string product = "Laptop";
            string brand = "TechPro";
            int rating = 4;
    
            string productLine = product;
            string brandLine = "1. " + brand + " (" + rating + ")";
            Console.WriteLine(productLine + "\n" + brandLine);
    

    Details worth knowing:

    • Strings are immutable. + never changes an existing string. It creates a new one. productLine = product makes both variables refer to the same text, and that is safe precisely because the text cannot be modified.
    • Left to right. The + operator is evaluated from left to right. 1 + 2 + "x" gives 3x, because the two numbers are added first. "x" + 1 + 2 gives x12, because after the first join the left side is a string. In this lesson the expression starts with a string, so the rating is always joined as text.
    • Escape sequences. "\n" is a newline. In a verbatim string, written with an @ before the quote, backslashes are not special, so @"a\nb" prints a, a backslash, an n and b on one line. This lesson uses the normal kind.
    • Alternatives. C# also offers interpolated strings, $"1. {brand} ({rating})", which build the same text. The lesson uses + to make each piece visible.
    • Console.WriteLine ends the line. The text passed to it is followed by one more newline, so the output ends cleanly after (4).

    Edge cases and common mistakes

    • Missing spaces. The most common error is writing "1." without the trailing space, or "(" without the space before it. The output must match exactly: 1. TechPro (4).
    • Forgetting the newline. Joining the two lines without "\n" prints Laptop1. TechPro (4) on a single line.
    • Quoting the variables. "brand" in quotes is the word brand, not the variable's value.
    • Other data. The same code gives Phone and 1. MegaCorp (5) when the variables hold "Phone", "MegaCorp" and 5.

    Testing

    • "Laptop", "TechPro", 4 give Laptop and 1. TechPro (4).
    • "Phone", "MegaCorp", 5 give Phone and 1. MegaCorp (5).
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free