Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Create a new string from a given string where the first and last characters will change their positions

C# Sharp Basic: Exercise-16 with Solution

Write a C# program to create a new string from a given string where the first and last characters will change their positions

C# Sharp Exercises: Create a new string from a given string where the first and last characters will change their positions

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;

public class Exercise16 {
  static void Main(string[] args)
        {
            Console.WriteLine(first_last("w3resource"));
            Console.WriteLine(first_last("Python"));
            Console.WriteLine(first_last("x"));
        }
       public static string first_last(string ustr)
        {
            return ustr.Length > 1
                ? ustr.Substring(ustr.Length - 1) + ustr.Substring(1, ustr.Length - 2) + ustr.Substring(0, 1) : ustr;
        }
}

Test Data: w3resource

Sample Output:

e3resourcw                                                             
nythoP                                                                 
x 

Flowchart:

Flowchart: C# Sharp Exercises - Create a new string from a given string where the first and last characters will change their positions

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program remove specified a character from a non-empty string using index of a character
Next: Write a C# program to create a new string from a given string (length 1 or more ) with the first character added at the front and back.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.