OneCompiler

Problem 2

204

/*Problem 2
Complexity: Low
You have been given a set of n strings in an array. Each string contains one or more words separated by spaces.
You have another string called “Search String”. You need to match the search string based on word boundary.

For example,

Below are the string elements in array (one element in each line)
Cecilio Johnes
Cordell Acott
Curtice Bleue
Dalis Menary
Emanuele Barstock
Emmy Bradane
Gian Cossington
Jamaal Danilevich
Kerri Wilce
Liuka Cowern
Merrel Cornuau
Marena Appleton
Marlee Stobo
Rochette Brew
Rudyard Abbate
Sharona Climo
Shirlee Titcumb
Taite Joriot
Taite Huntriss
Zach Simchenko

Search String s = "c”;
Output should be: String “c” (case insensitive) matching the first letter in each word. Following entries will be selected.
Cecilio Johnes
Cordell Acott
Curtice Bleue
Gian Cossington
Liuka Cowern
Merrel Cornuau
Sharona Climo

Search String s = "co”;
Output should be: String “co” (case insensitive) matching the first two letters in each word. Following entries will be selected.
Cordell Acott
Gian Cossington
Liuka Cowern
Merrel Cornuau*/

//Imports
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// Class
public class StartingWithString
{
	// Method to get the elements starting with search string
	public static void startingWith(String[] arr , String search_string) 
	{
			// To create Array List to store matched elements
			List<String> new_list = new ArrayList<String>();

			// To iterate over array
			for (int i = 0; i < arr.length; i++)
			{
				// To split the array element present at i'th index
				String[] child_arr = arr[i].split(" ");
				
				for (int j = 0; j < child_arr.length; j++)
				{
					//To check whether the entry starts with search string or not
					if (child_arr[j].toLowerCase().startsWith(search_string.toLowerCase()) || child_arr[j].toUpperCase().startsWith(search_string.toUpperCase()))
					{
						//To add matched elements in the list
						new_list.add(arr[i]);
						break;
						
					}//End of if-block
					
				}// End of for-loop
				
			} // End of outer for-loop
			
			// To print the matched entries
			System.out.println("Entries starting with " +search_string + " are : ");	
			
			//Using for-each loop
			for (String entry : new_list)
			{
				System.out.println(entry);
			}// End of for-each loop
			
	}// End of method
	
	// main method
	public static void main(String[] args) 
	{
		// object of the Scanner class
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter size of the array : ");
		
		// Reading size of array from user
		int arr_length = sc.nextInt();

		// Create array of given size
		String[] arr = new String[arr_length];
		
		// Object of the Scanner class
		Scanner sc1 = new Scanner(System.in);

		// Reading array elements from user
		for (int i = 0; i < arr.length; i++)
		{
			System.out.println("Enter array element " + i + "  : ");
			arr[i] = sc1.nextLine();

		}// End of for-loop
		
		//Reading search string from user
		System.out.println("Enter the string to search elements starting with it: ");
		String search_string = sc1.nextLine();
		
		//Calling method to get the elements starting with a search string
		StartingWithString.startingWith(arr, search_string);
		
		/* Author @Ayesha Sumayya Khanum */

	// End of main
	}
	
// Class exit
}