Q:

Write a C# Sharp program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character

0

Write a C# Sharp program to create a new string taking the first character from a given string and the last character from another given string. If the length of any given string is 0, use '#' as its missing character

Sample Output:

Hi
PP
JS
C#

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;

namespace exercises
{
   class Program
    {       
        static void Main(string[] args)
        {
            Console.WriteLine(test("Hello", "Hi"));
            Console.WriteLine(test("Python", "PHP"));
            Console.WriteLine(test("JS", "JS"));
            Console.WriteLine(test("Csharp", ""));
            Console.ReadLine();
        }

 public static string test(string s1, string s2)
        {
            string lastChars = String.Empty;

            if (s1.Length > 0)
            {
                lastChars += s1.Substring(0, 1);
            }
            else
            {
                lastChars += "#";
            }

            if (s2.Length > 0)
            {
                lastChars += s2.Substring(s2.Length - 1);
            }
            else
            {
                lastChars += "#";
            }

            return lastChars;
        }
   }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now