Write a program in C# Sharp to find the string which starts and ends with a specific character

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to find the string which starts and ends with a specific character.

Test Data :
The cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS'
Input starting character for the string : A
Input ending character for the string : M
Expected Output :
The city starting with A and ending with M is : AMSTERDAM

الأجوبة

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
class LinqExercise8  
    {  
        static void Main(string[] args)  
      {  
          string chst,chen;
          char ch;
            string[] cities =  
            {  
                "ROME","LONDON","NAIROBI","CALIFORNIA","ZURICH","NEW DELHI","AMSTERDAM","ABU DHABI", "PARIS"  
            };  
            Console.Write("\nLINQ : Find the string which starts and ends with a specific character : "); 
            Console.Write("\n-----------------------------------------------------------------------\n");	
            Console.Write("\nThe cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS' \n");	            
           Console.Write("\nInput starting character for the string : "); 
            ch = (char)Console.Read(); 
            chst=ch.ToString();
            Console.Write("\nInput ending character for the string : "); 
            ch = (char)Console.Read(); 
            chen=ch.ToString();
            var _result = from x in cities  
            where x.StartsWith(chst)  
            where x.EndsWith(chen)  
            select x;  
            Console.Write("\n\n");
            foreach(var city in _result)  
            {  
                Console.Write("The city starting with {0} and ending with {1} is : {2} \n", chst,chen,city);  
            }  
            Console.ReadLine();  
        }  
    }
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...