Start learning free

LESSON 67 OF 289 · STRINGS - CHALLENGES

Concat First and Last Char in C#: Indexing a String

Practice Read the full explanation

Given a string s, return a new string made of two characters: the first character of s, followed by its last character.

 

Example 1:

Input: s = "hello"
Output: "ho"
Explanation: The first character is "h" and the last one is "o".

Example 2:

Input: s = "Hello World!"
Output: "H!"
Explanation: Every character counts, so the last one is the "!", not the "d". The "H" stays upper case.

Example 3:

Input: s = "ab"
Output: "ab"
Explanation: With only two characters, the first is "a" and the last is "b".

 

Constraints:

  • s has at least 2 characters.
  • s contains English letters, digits, spaces and punctuation, and doesn't start or end with a space.
STEP 1 OF 1
    Ctrl + Enter to check

    C# · STRINGS - CHALLENGES

    Concat First And Last Char in C#, explained

    Problem overview

    The method ConcatFirstAndLastChar receives a string s and returns a new two-character string: the first character of s, then its last character. For "hello" the result is "ho". For "Hello World!" it is "H!", because the last character is the exclamation mark, and every character counts, spaces and punctuation included. For "ab" the result is "ab".

    Approach: two indexes and a join

    A C# string can be indexed like an array: s[i] is the character at position i, and its type is char. Positions start at 0, so the first character is s[0]. The string's Length is the number of characters, so the last position is s.Length - 1. The lesson guarantees at least two characters, so both positions always exist.

    The only real question is how to join two char values into a string.

    Why the empty string comes first

    A char is a small number underneath (its character code), and + between two chars adds those numbers. Adding 'h' and 'o' gives an int, not text:

    char a = 'h', b = 'o';
    Console.WriteLine(a + b);
    Console.WriteLine("" + a + b);

    This prints:

    215
    ho

    The first line is 104 + 111. In the second line, "" + a is already a string, so each later + appends a character as text. That is why the solution starts from "". Calling .ToString() on the first char works too.

    The solution in C#

    class Solution {
        public string ConcatFirstAndLastChar(string s) {
            return "" + s[0] + s[s.Length - 1];
        }
    }

    The expression is evaluated left to right: "" + s[0] is a string, and the string plus s[s.Length - 1] is the final result. Strings in C# are immutable, so nothing is changed in s; each + builds a new string.

    Step by step

    The lesson has one step, and it can be read as three small decisions.

    1. Start from "". This makes the whole expression a string expression, so every following + means "append text".
    2. Add the first character. s[0] exists for every non-empty string, and the lesson's strings have at least two characters.
    3. Add the last character. Its index depends on the input, so it is computed: s.Length - 1. For "hello" the length is 5 and the index is 4, which is 'o'. For "Hello World!" the length is 12 and the index is 11, the exclamation mark.

    The method never loops and never looks at the middle of the string, so the time it takes does not depend on how long s is.

    Other ways to write it

    Modern C# has a few equivalent spellings. They all give "H!" for "Hello World!":

    string a = s[0].ToString() + s[^1];
    string b = $"{s[0]}{s[^1]}";
    string c = string.Concat(s[0], s[s.Length - 1]);

    Here s[^1] is an index counted from the end: ^1 is the last character, ^2 the one before it. The second line uses string interpolation, where each {...} is converted to text and inserted. Use whichever you find clearest; the lesson's solution relies only on plain indexes and +, which work the same in every version of the language.

    Edge cases and common mistakes

    • Forgetting the leading "". s[0] + s[s.Length - 1] compiles, but it is an int, so returning it from a method declared as string is a compile error.
    • Using s.Length as the last index. The last index is s.Length - 1; s[s.Length] throws an IndexOutOfRangeException.
    • Two characters. With "ab" the first and last positions are 0 and 1, and the result is the same string.
    • Case and punctuation. Nothing is converted: "Hello World!" keeps its capital H and gives "H!".

    Testing

    InputResult
    "hello""ho"
    "Hello World!""H!"
    "ab""ab"
    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