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.
- Start from
"". This makes the whole expression a string expression, so every following+means "append text". - Add the first character.
s[0]exists for every non-empty string, and the lesson's strings have at least two characters. - 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 anint, so returning it from a method declared asstringis a compile error. - Using
s.Lengthas the last index. The last index iss.Length - 1;s[s.Length]throws anIndexOutOfRangeException. - 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 capitalHand gives"H!".
Testing
| Input | Result |
|---|---|
"hello" | "ho" |
"Hello World!" | "H!" |
"ab" | "ab" |
