Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is an array of words. I want to get the largest set of words with the letter 'a' at a specific position and delete the rest of the words in the array. For example, i want to get the output of {alga, ajka, aoia} as this is the largest set of words with 'a' at a specific position. Im really confused any help would really be appreciated.

What I have tried:

static void Main(string[] args)
       {
           string[] words = { "alga", "apal", "aeal", "ajka", "aeal", "aoia", "aaul", "gaal", "laaj", "lata", "laoa" };

           char letter = 'a';
           List<String> wordFamilies = new List<String>();

           foreach (String word in words.Where(word => word.Contains(letter)))
           {
               wordFamilies.Add(word);
           }

           Console.WriteLine(String.Join(" ", wordFamilies));

       }
Posted
Updated 31-Oct-21 4:40am
v5
Comments
[no name] 30-Oct-21 15:54pm    
You haven't specified a "position" (e.g. word[1] == 'a'); you've simply selected all that "Contains" 'a'.
Member 15405921 30-Oct-21 16:26pm    
Could you please show me how-to based on my code?

1 solution

using System;
using System.Collections.Generic;
					
public class Program
{
	public static void Main()
	{
           string[] words = { "alga", "ajka", "aoia", "apal", "aeal", "aatl", "aaul", "gaal", "laaj", "lata", "laoa" };

           char letter = 'a';
		   int pos = 3;
           List<string> wordFamilies = new List<string>();

           foreach (var word in words)
           {
			   if (pos < word.Length && word[pos] == letter)
                 wordFamilies.Add(word);
           }	
		
		   Console.WriteLine(string.Join(",", wordFamilies));
	}
}
 
Share this answer
 
v3
Comments
Member 15405921 30-Oct-21 17:36pm    
RickZeeland I have tried your code but it returns {"alga", "ajka", "aoia", "lata", "laoa"} , why so?
RickZeeland 31-Oct-21 2:26am    
As you mentioned "words with the letter 'a' at a specific position" it searches for the letter at position 3, which is the fourth letter as arrays in C# start from 0.
BillWoodruff 31-Oct-21 9:05am    
imho, this OP needs education, not having his code written for him.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900