w3resource
C# Sharp Programming Exercises

C# Sharp Exercises: Create a new string of four copies, taking last four characters from a given string

C# Sharp Basic: Exercise-32 with Solution

Write a C# program to create a new string of four copies, taking last four characters from a given string. If the length of the given string is less than 4 return the original one.

C# Sharp Exercises: Create a new string of four copies, taking last four characters from a given string

Sample Solution:-

C# Sharp Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Exercise32 {
  static void Main(string[] args)
        {
           string str; 
           int l= 0;
           Console.Write("Input a string : ");
           str = Console.ReadLine();
           if (str.Length>4)
           {
              Console.WriteLine(str.Length < 4 ? str + str + str : str.Substring(str.Length - 4)+ str.Substring(str.Length - 4) + str.Substring(str.Length - 4) + str.Substring(str.Length - 4));
           }
        }
}

Sample Output:

Input a string : The quick brown fox jumps over the lazy dog.          
dog.dog.dog.dog.

Flowchart:

Flowchart: C# Sharp Exercises - Create a new string of four copies, taking last four characters from a given string

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# program to multiply corresponding elements of two arrays of integers.
Next: Write a C# program to check if a given positive number is a multiple of 3 or a multiple of 7.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming