C#: Create a new string with the last char added at the front and back of a given string of length 1 or more
Last Character at Front and Back
Write a C# Sharp program to create a string with the last char added at the front and back of a given string of length 1 or more.
Visual Presentation:

Sample Solution:
C# Sharp Code:
using System;
// Namespace declaration
namespace exercises
{
// Class declaration
class Program
{
// Main method - entry point of the program
static void Main(string[] args)
{
// Calling the 'test' method with different strings and displaying the returned values
Console.WriteLine(test("Red")); // Output: "dRedd"
Console.WriteLine(test("Green")); // Output: "nGreenn"
Console.WriteLine(test("1")); // Output: "11"
Console.ReadLine(); // Keeping the console window open
}
// Method to perform a specific string transformation
public static string test(string str)
{
// Extracts the last character of the input string
var s = str.Substring(str.Length - 1);
// Concatenates the last character, the input string, and the last character again
return s + str + s;
}
}
}
Sample Output:
dRedd nGreenn 111
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C# program to add the second character to the front and back of a string of at least two characters.
- Write a C# program to surround the string with both the first and last characters swapped.
- Write a C# program to repeat the last character at the front and in the middle of the string.
- Write a C# program that surrounds a string with its reversed last three characters.
Go to:
PREV : Four Copies of First Two Characters.
NEXT : Multiple of 3 or 7.
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.