I need to write a program to count the number of strings where the string length is 2 or more and the first and last character are same as these in certain list with strings ["people", "desk", "orange", "yellow", "carrot", "pineapple"]
example: Input: "pe" , result- 2 -same as “people” и “pineapple ”
For now I did this, it search if the first and last chars are the same:
List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple"};
int count = 0;
foreach (var c in stringsList)
{
if (c.Length > 1 && c[0] == c[-1])
{
count += 1;
}
}
Console.WriteLine(count);
You can use linq for this purpose. So you need to
So code looks like this:
List<string> stringsList = new List<string> { "people", "desk", "orange",
"yellow", "carrot", "pineapple" };
int count = 0;
var input = "pe";
count = stringsList.Where(s => s.Length > 2
&& s[0] == input[0]
&& s[s.Length - 1] == input[1])
.Count();
Try the answer below:
//I'll let you input however you want to. Probably use console.ReadLine();
string input = Console.ReadLine();
int count;
List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple"};
foreach (string item in stringsList)
{
if (input.Length > 1 && item.Length > 1)
{
if(input[input.Length - 1] == item[item.Length - 1])
{
//Same Last character
if(input[0] == item[0])
{
//Same first character
count += 1;
}
}
}
}
Console.WriteLine(count);
You can query with a help of Linq :
using System.Linq;
...
List<string> stringsList = new List<string> {
"people", "desk", "orange", "yellow", "carrot", "pineapple"
};
string input = "pe";
int count = stringsList
.Count(word => word.Length >= 2 &&
word[0] == input[0] &&
word[word.Length - 1] == input[input.Length - 1]);
Or even
int count = stringsList
.Count(word => word.Length >= 2 &&
word.First() == input.First() &&
word.Last() == input.Last());
If you are using C#8 and .Net Core 3, you can use the new "index from end" operator to get at the last character of a string, to simplify the code somewhat.
For example, the last character of a string str
is given by str[^1]
.
So you could use the Linq Count()
operator to count all the strings of length >=2 where the first and last characters match the target; for example:
using System;
using System.Collections.Generic;
using System.Linq;
namespace CoreConsole
{
public static class Program
{
static void Main()
{
List<string> stringsList = new List<string> { "people", "desk", "orange", "yellow", "carrot", "pineapple" };
string search = "pe";
int n = stringsList.Count(str =>
str.Length >= 2
&& str[ 0] == search[0]
&& str[^1] == search[1]);
Console.WriteLine(n);
}
}
}
Thanks, this is what I did:
foreach (var c in stringsList)
{
if (c.Length > 1 && c[0] == input[0] && c[c.Length-1] == input[1])
{
count += 1;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.