0

I'm actually new in java and don't know the methods very well and I'm wonder how can I find a messed up word in a string like:

the string is : deadline is Deadline, so try not to extend DeaDLINE!!!

and the word I'm looking for is "liNedeAd"

yeah it's messed up and I wanna the program Ignore case too!

String str, word;
int count= 0;
str=in.nextLine();word=in.nextLine();

String a[] = str.split(" ");
for(int i = 0;i<a.length;i++) {
    // what should I do here?
    if (word.equals(a[i]))
        count++;
}

System.out.println(count);

Really appreciate tell me the methods that I can use.

Mustafa Poya
  • 1,814
  • 1
  • 11
  • 20
  • When you say 'the word I'm looking for is ...'. Are you expecting it to say `true` or `false` for the example string you gave? And are you just wanting the letters (case ignoring) in the entire `String`? Or do you need them to all be in the same word, and with no other letters as well? – BeUndead Nov 30 '20 at 17:16
  • https://www.geeksforgeeks.org/check-whether-two-strings-are-anagram-of-each-other/ – Venkat Ramana Nov 30 '20 at 17:17
  • Yeah actually I want to check the letters case ignoring in the entire **String**. and if it came with another letters like **"deadlinemage"** i don't wanna it be count as **deadline** – Einar Johnson Nov 30 '20 at 17:19
  • 1
    Your goal seems to be to check for the presence of an anagram of "deadline", ignoring case. Correct? – J Woodchuck Nov 30 '20 at 17:51
  • Yes That is correct, it doesn't matter word be **Deadline** or **linedEad** I just want to program tells me how many times word repeated in **String** – Einar Johnson Nov 30 '20 at 18:01

2 Answers2

1

If you want to ignore cases, this would probably be the best option:

if(word.equalsIgnoreCase(a[i])
   count++;

Another, less efficient way of doing it would be to use the toLowerCase() method on both strings.

dreamcrash
  • 36,542
  • 23
  • 64
  • 87
1

String#equalsIgnoreCase

Use this to compares a String to another ignoring case considerations.

There are many ways to solve your problem. I have listed a few of them and I suggest you to try some more ways once you have understood these ones.

import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String str = in.nextLine();
        System.out.print("Enter the word: ");
        String word = in.nextLine();
        long count = 0;

        // Using Stream API
        count = Arrays.stream(str.split("\\s+|\\p{Punct}")).filter(w -> w.equalsIgnoreCase(word)).count();
        System.out.println(count);

        // Pre-Java8 way
        count = 0;
        for (String w : str.split("\\s+|\\p{Punct}")) {
            if (w.equalsIgnoreCase(word)) {
                count++;
            }
        }
        System.out.println(count);

        // Using regex
        count = 0;
        Matcher matcher = Pattern.compile("(?i)\\b" + word + "\\b").matcher(str);
        while (matcher.find()) {
            count++;
        }
        System.out.println(count);
    }
}

A sample run:

Enter the sentence: deadline is Deadline, so try not to extend DeaDLINE!!!
Enter the word: deadline
3
3
3
  1. The regex, \s+|\p{Punct} means one or more whitespace characters or a punctuation character
  2. (?i) in the beginning of a regex pattern makes it case-insensitive.
  3. \b specifies a word boundary.

If you are looking to match anagrams, you can define a function to find if the two words are anagram and the rest of the processing will be same as above.

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the sentence: ");
        String str = in.nextLine();
        System.out.print("Enter the word: ");
        String word = in.nextLine();
        long count = 0;

        // Using Stream API
        count = Arrays.stream(str.split("\\s+|\\p{Punct}")).filter(w -> anagrams(w, word)).count();
        System.out.println(count);

        // Pre-Java8 way
        count = 0;
        for (String w : str.split("\\s+|\\p{Punct}")) {
            if (anagrams(w, word)) {
                count++;
            }
        }
        System.out.println(count);
    }

    static boolean anagrams(String str1, String str2) {
        if (str1.length() == str1.length()) {
            // Get the char[] from strings after converting them to the same case
            char[] arr1 = str1.toLowerCase().toCharArray();
            char[] arr2 = str2.toLowerCase().toCharArray();
            Arrays.sort(arr1);
            Arrays.sort(arr2);
            return (Arrays.equals(arr1, arr2));
        }
        return false;
    }
}

Output:

Enter the sentence: deadline is Deadline, so try not to extend DeaDLINE!!!
Enter the word: liNedeAd
3
3

Important links:

  1. Lesson: Regular Expressions
  2. Reference - What does this regex mean?
  3. Processing Data with Java SE 8 Streams, Part 1
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72