Q:

Write a C# Sharp program to check if a given string begins with 'abc' or 'xyz'. If the string begins with 'abc' or 'xyz' return 'abc' or 'xyz' otherwise return the empty string

0

Write a C# Sharp program to check if a given string begins with 'abc' or 'xyz'. If the string begins with 'abc' or 'xyz' return 'abc' or 'xyz' otherwise return the empty string

Sample Output:

abc
abc

xyz
xyz

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("abc"));
            Console.WriteLine(test("abcdef"));
            Console.WriteLine(test("C"));
            Console.WriteLine(test("xyz"));
            Console.WriteLine(test("xyzsder"));
            Console.ReadLine();
        }

 public static string test(string s1) 
        {
          if (s1.StartsWith("abc"))
            {
                return "abc";
            }
            else if (s1.StartsWith("xyz"))
            {
                return "xyz";
            }
            else
            {
                return String.Empty;
            }
        }
   }
}

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