-4

Input: [the, and, land, wander, dreams]

Substring: "and"

Output: 3


I need to find all the occurrences of an array that contain a certain substring but all I found was the word itself. For example, I want it to count the "and" in the word "land" and "wander" as well. I don't know how to do that. Please help!

zoe
  • 1
  • 6
    Hi, SO is a place where people put their own personal time to assist you figuring out whats wrong with what you've tried, not hand you solutions. Feel free to edit your post with what code you have so far so you can get some assistance. – Nergon Sep 28 '21 at 20:07
  • Class `String` has several methods to lookup for a substing inside the string: [`String::contains`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#contains-java.lang.CharSequence-), [`String::indexOf(String look)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#indexOf-java.lang.String-), [`String::lastIndexOf(String look)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#lastIndexOf-java.lang.String-), [`String::matches(String regex)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#matches-java.lang.String-) – Nowhere Man Sep 28 '21 at 22:21

1 Answers1

2

EDIT: Updated the code. What about:

    int cnt=0;
    String[] input = {"the", "and", "land", "wander", "dreams"};
    for (String str : input){
        if (str.contains("and")){
            cnt++;
        }
    }
    System.out.println(cnt);

This code should work for you. But keep in mind following - Exbow is right, next time your question might be considered as useless and will be closed.

DRINK
  • 452
  • 1
  • 6
  • 15