C#: Create a new string from a given string with the first character added at the front and back
C# Sharp Basic: Exercise-17 with Solution
Add First Character to Front and Back
Write a C# program to create a string from a given string (length 1 or more) with the first character added at the front and back.
Sample Solution:-
C# Sharp Code:
using System;
using System.Text;
// This is the beginning of the Exercise17 class
public class Exercise17 {
// This is the main method where the program execution starts
static void Main(string[] args)
{
string str; // Declaring a variable to store the input string
// Prompting the user to input a string
Console.Write("Input a string : ");
str = Console.ReadLine(); // Reading the input string from the user
// Checking if the input string has a length of at least 1 character
if (str.Length >= 1)
{
var s = str.Substring(0, 1); // Extracting the first character of the string
// Printing the string with the first character added at the beginning and end
Console.WriteLine(s + str + s);
}
}
}
Sample Output:
Input a string : The quick brown fox jumps over the lazy dog. TThe quick brown fox jumps over the lazy dog.T
Flowchart:
C# Sharp Code Editor:
Previous: Write a C# program to create a new string from a given string where the first and last characters will change their positions
Next: Write a C# program to check two given integers and return true if one is negative and one is positive.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/basic/csharp-basic-exercise-17.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics