0

WAP to input a sentence and print how many times "is" appears in that sentence [Java] Must only use string functions Must only use scanner class

No array functions or splitting the sentence

  • 1
    Please share what you have tried and then ask if you are stuck at someplace. No one is here to solve your homework. – backdoor May 09 '20 at 05:54
  • I have been given to complete 50 programs i was stuck in this so i asked here , i don't know where to start . – Cracking X Patching May 09 '20 at 05:57
  • Does this help? [Count the number of Occurrences of a Word in a String](https://stackoverflow.com/questions/22566503/count-the-number-of-occurrences-of-a-word-in-a-string) By the way, what is WAP? – Abra May 09 '20 at 10:34

1 Answers1

0
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input;
        input = scan.nextLine();
        int index = input.indexOf("is");
        int count = 0;
        while (index != -1) {
            count++;
            input = input.substring(index + 1);
            index = input.indexOf("is"); 
        }
        System.out.println("No of *is* in the input is : " + count);
    } 
}
hata
  • 10,652
  • 6
  • 37
  • 62
  • How many times does the word _is_ appear in the following sentence? _This list is long._ Now how many times does your code say that it appears? – Abra May 09 '20 at 10:37