Start learning free

LESSON 68 OF 289 · STRINGS - CHALLENGES

Amazing to Not Amazing in C#: Substring and String Concatenation

Practice Read the full explanation

Given a word s that starts with the letter a, return "not " followed by s without its first character. That's how "amazing" becomes "not mazing"!

 

Example 1:

Input: s = "amazing"
Output: "not mazing"
Explanation: Without its first character, "amazing" is "mazing". "not " goes in front of it, with its space.

Example 2:

Input: s = "aMAZING"
Output: "not MAZING"
Explanation: Only the first character is removed. The other characters stay as they are, upper case included.

Example 3:

Input: s = "an"
Output: "not n"
Explanation: Removing the "a" leaves just "n".

 

Constraints:

  • s has at least 2 characters and starts with a lower-case a.
  • s is one word made of English letters, lowercase or uppercase (no spaces).
STEP 1 OF 1
    Ctrl + Enter to check

    C# · STRINGS - CHALLENGES

    Amazing To Not Amazing in C#, explained

    Problem overview

    The method AmazingToNotAmazing receives a word s that starts with a lower-case a. It returns "not " (with its space) followed by s without its first character. So "amazing" becomes "not mazing", "aMAZING" becomes "not MAZING" and "an" becomes "not n". Only the first character is dropped; the case of the others stays as it is.

    Approach: drop one character, add a prefix

    Two pieces are needed: the fixed text "not ", and the word minus its first letter. The second piece is what Substring gives. s.Substring(1) returns a new string that starts at index 1 and runs to the end of s, so it keeps every character except the first. The two pieces are joined with +.

    Substring with one argument

    Substring has two forms. Substring(start) goes from start to the end. Substring(start, length) takes length characters. This lesson only needs the first form:

    Console.WriteLine("amazing".Substring(1));
    Console.WriteLine("an".Substring(1));
    Console.WriteLine("amazing".Substring(7).Length);
    mazing
    n
    0

    Starting exactly at Length gives an empty string. Starting past it throws an ArgumentOutOfRangeException. Since the lesson's words have at least two characters, Substring(1) is always safe.

    The solution in C#

    class Solution {
        public string AmazingToNotAmazing(string s) {
            return "not " + s.Substring(1);
        }
    }

    Strings are immutable in C#, so Substring never changes s: it builds a new string, and + builds another one from the two parts.

    Step by step

    The lesson is a single step, made of two decisions.

    1. Keep everything after the first character. For "amazing", index 0 is 'a' and index 1 is 'm', so Substring(1) returns "mazing". The letter at index 0 is simply left out. The method never checks which letter it removes; it only relies on the word starting with a, as the lesson promises.
    2. Put "not " in front. The prefix is a string literal, so the + operator joins it with the substring as text. The order matters: the prefix goes on the left.

    Because the substring result is a new string, you can also keep it in a variable first, for example string rest = s.Substring(1);, and return "not " + rest. The one-line version does the same thing.

    Other ways to write it

    C# 8 and later also accept a range as an index. s[1..] means "from index 1 to the end" and gives the same string as s.Substring(1). Both of these return "not mazing" for "amazing":

    string a = "not " + s[1..];
    string b = $"not {s[1..]}";

    The second line uses string interpolation: the part in braces is evaluated and inserted into the text, so the space inside the quotes still matters. The lesson's solution uses Substring, which is the method you will meet in most existing C# code.

    Edge cases and common mistakes

    • The space. It belongs inside "not ". With "not" the result for "amazing" would be "notmazing".
    • Changing the case. Nothing here calls ToLower or ToUpper; "aMAZING" keeps its capitals.
    • Two-letter words. "an" leaves one letter, "n", and the result is "not n".
    • Index 0 instead of 1. s.Substring(0) returns the whole word, which would give "not amazing".

    The method does not test whether the word really starts with a. If it is given "hello", it returns "not ello", because it removes the first character whatever it is. That is fine here, since the lesson's inputs always start with a lower-case a, but it is worth remembering: a method that only needs to work for a promised kind of input can stay this short, and a method for unknown input would first have to check it.

    Testing

    InputResult
    "amazing""not mazing"
    "aMAZING""not MAZING"
    "an""not n"
    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