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:
Flowchart:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework