Advertisement
Guest User

lettersMatchIgnoreCase

a guest
Feb 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package Curs19;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class Ex2Filter {
  7.  
  8. public static String[] lettersMatchIgnoreCase(String[] arrTLC, String key) {
  9. String[] answear = new String[arrTLC.length];
  10. int numElem = 0;
  11. char[] ch = new char[key.length()];
  12. ch = key.toCharArray();
  13. for (int i = 0; i < arrTLC.length; i++) {
  14. for (int j = 0; j < arrTLC[i].length(); j++) {
  15. for (int k = 0; k < ch.length; k++) {
  16. if (ch[k] == arrTLC[i].charAt(j) && j == k) {
  17. answear[numElem] = arrTLC[i];
  18. numElem++;
  19. break;
  20. }
  21. }
  22.  
  23. }
  24. }
  25. answear = Arrays.copyOf(answear, numElem);
  26. return answear;
  27. }
  28.  
  29. public static void printArray(String[] answear) {
  30. for (int i = 0; i < answear.length; i++) {
  31. System.out.print(answear[i] + " ");
  32. }
  33. }
  34.  
  35. public static String[] convertToLowerCase(String[] array) {
  36. String[] answear = new String[array.length];
  37. for (int i = 0; i < array.length; i++) {
  38. answear[i] = array[i].toLowerCase();
  39. }
  40.  
  41. return answear;
  42. }
  43.  
  44. public static void main(String[] args) {
  45. Scanner sc = new Scanner(System.in);
  46. String[] array = new String[2];
  47. String stopStr = "stop";
  48. int numOfStr = 0;
  49.  
  50. System.out.println("Enter the strings:");
  51. String s = sc.next();
  52. while (!s.equals(stopStr)) {
  53. array[numOfStr] = s;
  54. numOfStr++;
  55.  
  56. if (numOfStr == array.length) {
  57. array = Arrays.copyOf(array, array.length * 2);
  58. }
  59.  
  60. s = sc.next();
  61. }
  62.  
  63. array = Arrays.copyOf(array, numOfStr);
  64.  
  65. System.out.println("Enter the key word:");
  66. String key = sc.next();
  67.  
  68. String[] arrTLC = convertToLowerCase(array);
  69.  
  70. String[] answear = lettersMatchIgnoreCase(arrTLC, key);
  71.  
  72. printArray(answear);
  73. //System.out.println();
  74. //removeDuplicate(answear);
  75.  
  76. //Exemplu:
  77. //strings: border ant drop Fly racoon class Plane
  78. //key: plot
  79. //RETURN: ["drop", "Fly", "class", "Plane"]
  80. sc.close();
  81. }
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement