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: Find the string which starts and ends with a specific character

C# Sharp LINQ : Exercise-8 with Solution

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

Sample Solution:-

C# Sharp Code:

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();  
        }  
    }
	

Sample Output:

LINQ : Find the string which starts and ends with a specific character :                                      
-----------------------------------------------------------------------                                       
The cities are : 'ROME','LONDON','NAIROBI','CALIFORNIA','ZURICH','NEW DELHI','AMSTERDAM','ABU DHABI','PARIS'  
                                                                                                     
Input starting character for the string : N                                                                   
Input ending character for the string : I                                                                     

The city starting with N and ending with I is : NAIROBI                                                       
The city starting with N and ending with I is : NEW DELHI

Pictorial Presentation:

C# Sharp LINQ: Find the string which starts and ends with a specific character.

Flowchart:

Flowchart: LINQ : Find the string which starts and ends with a specific character

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to display numbers, multiplication of number with frequency and the frequency of number of giving array.
Next: Write a program in C# Sharp to create a list of numbers and display the numbers greater than 80 as output.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.